Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer EXC_BAD_ACCESS

I am trying to pause the AVAudioPlayer if it is currently playing. When I debug my code and check AVAudioPlayer, I see it is allocated. When I try to access a method/property on it (i.e. myAudioPlayer isPlaying), I get an EXC_BAD_ACCESS error. This happens if the AVAudioPlayer does not have a sound loaded. Is there a way I can do a check to see if it has loaded a sound? I tried accessing myAudioPlayer.data, but I still get the same error.

like image 428
user472292 Avatar asked Oct 06 '11 04:10

user472292


3 Answers

For me it was because I wasn't calling the one of the designated initialisers. I was instantiating it with AVAudioPlayer() instead of the designated initialisers which are public init(contentsOfURL url: NSURL) throws and public init(data: NSData) throws

like image 103
villy393 Avatar answered Nov 11 '22 05:11

villy393


As of iOS 13, make sure you are removing the initialization on AVAudioPlayer before asigning it with AVAudioPlayer(contentsOf: URL(...))

i.e. change var audioPlayer = AudioPlayer() to var audioPlayer: AVAudioPlayer!

like image 25
Reid Svntn Avatar answered Nov 11 '22 05:11

Reid Svntn


I guess you have to use prepareToPlay method to find whether it has loaded or not.

like image 3
Ilanchezhian Avatar answered Nov 11 '22 04:11

Ilanchezhian