EXC_BAD_ACCESS when using AVAudioPlayer

Hello,


I am trying to use an AVAudioPlayer to play a song from iTunes, but I'm having a lot of difficulty.


Whenever I try to do something to my AVAudioPlayer even after giving it a URL, such as pausing it or trying to play at a certain time, my app crashes with EXC_BAD_ACCESS. I've looked online and it seems ike this is because I haven't put a URL in, but I have already done that.


Let me run you through my code.


First, I make an AVAudioPlayer in the class-level.

var player = AVAudioPlayer()


Next, in a function called a short time after the viewDidLoad, I run this code.


//Gathering the song to play

        let songs = MPMediaQuery.songs()

       let predicateByGenre = MPMediaPropertyPredicate(value: "[EXACT SONG TITLE]", forProperty: MPMediaItemPropertyTitle)

        songs.filterPredicates = NSSet(object: predicateByGenre) as? Set
        
        let url = (songs.items![0]).assetURL
        do {
            try player = AVAudioPlayer(contentsOf: url!)
        } catch {
            fatalError("Could not load the file when trying to load a song in wander mode")
        }
        
        //Updating the nowPlayingItem
        nowPlayingItem = songs.items![0]
       //nowPlayingItem is an MPMediaItem variable created so that the code can find out attributes about the current song when it needs to such as title, artist, etc.

        //Playing the player
        NSLog("Playing!")
        player.play()

Now, the player should hopefully have a url assigned to it with the song I have in my iTunes library I want it to play.


Now back to the viewDidLoad. In it, I have a small chunk of code:

delay(5, completion: {
            self.player.pause()
        })

The delay function is a function that, obivously, delays the code in completion argument by a set time defined in the argument just before.


Pausing the player seems like an easy thing to do, but when I do, the app crashes with EXC_BAD_ACCESS on the line where I try to pause it.

I don't understand what I going on; the player should have a url and shouldn't cause a bad access. Does anyone know what to do in this situation? Thanks in advance. Any help is appreciated.

Accepted Reply

So I actually found out why this is happening and fixed it. It turned out to be that my before the player was given a song, my code went out into a different file to do a few things and created a new instance of viewcontroller when it came back and tried to edit player. Thank you both for your help!

Replies

I found another solution:
Change

var player = AVAudioPlayer()

to

var player: AVAudioPlayer!

It's fix problem with error Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) for iOS 13.1.2

You are right, that is the most important part about this sort of issues.


By the way, I would use `AVAudioPlayer?` instead of `AVAudioPlayer!`.

Yes, possible use "?". But in this case I used "!", I'm waiting any value without nil.

Even if I wait non-nil value, I use `?`. And then compiler forces us to check if it is properly initialized or not. Which prevents many easy mistakes.

Thank you a lot!