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