It seems that a lot of games on the iPhone and iPod touch do not let you listen to your music AND play the games sound effects at the same time. You either have to turn the sound off in the prefs to listen to music or the music simply fades out when the game starts. Not ideal right? I know, my first three games killed the the iPod music. The good news is that Apple provides a way for both the iPod music and iPhone game sounds to mix in real time. It’s called Audio Sessions and the following code will mix your sound effects and iPod music in real time.
OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, self);
if (result) {
// Init error, handle error here
}else {
UInt32 category = kAudioSessionCategory_AmbientSound;
result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(category), &category);
if (result) {
// set audio session error, handle error here
}else {
result = AudioSessionSetActive(true);
if (result) {
// Set audio session active error, handle error here
}
}
}
I put a version of this snippet in my applicationDidFinishLaunching method so I can start playing background music right away.
Today I released my fourth game, Aerolite, to the App store. It’s currently in review and hopefully will be released in about a week. It lets you listen to the music on your iPod and play the game with sound effects using the code listed above. Now it’s time to update the rest of my games to handle music in the same way.
Entries (RSS)
[...] Link to original article [...]
[...] Sessions, but the documentation for that was huge and confusing. Fortunately Andrew pointed me at this post from Mark at Sputnik Games which solved my issue immediately just by copying and pasting 14 lines [...]
[...] time as music playback via iPodMusicPlayer and had been running into problems until I found this blog post from Sputnik Games which solved my problem. Add the following code to the [...]