Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if other apps are playing background audio?

I am making a game for iphone/ipad, and don't want my background music to interfere with music the user might already be playing.

I know I can get the state of the iPod app using MPPlayerController, but how would my app figure out if something else, like Pandora, was playing background audio on iOS4?

like image 726
gotung Avatar asked Nov 30 '25 06:11

gotung


1 Answers

I finally figured this out. I had seen plenty of examples, but couldn't ever get them to work.

Most of the examples out there don't point out that for something like:

    UInt32   propertySize, audioIsAlreadyPlaying;

propertySize = sizeof(UInt32);
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);

to work, the proper framework to add to you app is AudioToolbox. I kept trying to add other audio frameworks and always missed that one.

Once you add that frame work (and import it into the code file in question) just test to see if audioIsAlreadyPlaying is zero or one.

like image 69
gotung Avatar answered Dec 02 '25 21:12

gotung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!