In using all of the great new apps appearing on the App Store, a problem I'm often running into is that some apps, typically games, will interrupt the music that I'm listening to with and begin playing their own splash screen sounds or background music. Sometimes it happens for a short sound effect and nothing else, which is doubly annoying. While it is possible to re-override the new application's sound by double-pressing the home button and pressing play again, it is a step that shouldn't be necessary.

With the 2.0 firmware there wasn't any way around interrupting users' music, as Apple provided no way of even checking if any music was already playing. Thanks to the new iPhone 2.1 firmware, we can now do this! Moreover, it is simple:

UInt32 isPlaying;
UInt32 propertySize = sizeof(isPlaying);

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(
kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &isPlaying);

If isPlaying is 0, no music is playing. If it is any other value, please let users continue to listen to what they chose to play! It is remarkably easy to do, so there should be no excuses about additional complexity.

I've also created a small sample program that demonstrates checking music status on the phone. It has to be run on a device, as checking the kAudioSessionProperty_OtherAudioIsPlaying property doesn't return successfully in the simulator. The sample code also checks music playback status when the program reactivates, such as after the user double-presses the home button to bring up the music status. This provides a chance to start your own application's music later on should the user desire. The source is public domain, use it as you see fit.

Download MusicStatus Source

4 comments:

Karoly Domonyi at 2008-11-20 03:24:06 -0500

Hi, Your site is one of my favorites seen around blog explosion. Keep up the good work. I enjoy reading your blog. It is great to find someone who can find the fun things in life! I wish you all the best in all years. I look forward to developing a friendship and networking with you. Let me know if there is something I can do to assist you with your business free. With Regards, Karoly Domonyi http://www.ariestrade.com

BellaKitty at 2009-07-13 23:23:58 -0400

I couldn't live without my iphone! I know thats sad, but it's true.

me at 2009-09-15 09:03:19 -0400

unfortunately this feature does not work anymore, thanks apple for your kind castration..

Jan Babiuch-Hall at 2009-12-24 17:27:49 -0500

I really wish there was to make sure that Apple would read your blog and take cue. Chax is something that should come stock on iChat. Warp should also be a native part of OS X. The above should be a part of every app, it should even be an option required by the SDK. After you're done with school, you should see if you can work for Apple. You'd be an excellent addition.