Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone AVAudioPlayer stopping background music

So I've just noticed that on my iPod Touch, when my app triggers a short wav file to play using AVAudioPlayer, the music gets paused. Is this normal?

I can't find any reference to this, and it seems like it would be noted somewhere. Is there a way to keep the music going while I play my sounds?

like image 587
Kenny Winker Avatar asked Nov 04 '09 09:11

Kenny Winker


1 Answers

Basically, every app is assigned an audio session which is modelled as a singleton class which you can get at application launch and set parameters to. The way I fixed the same problem was through a single line of code placed at applicationDidFinishLaunching:

Objective-C:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

Swift 2/3:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
like image 80
Michael M Avatar answered Sep 28 '22 12:09

Michael M