about:benjie

Random learnings and other thoughts from an unashamed geek

iPhone Music and Sound Effects in Parallel

| Comments

I was trying to find out how to use [MPMusicPlayerController iPodMusicPlayer] and AVAudioPlayer in parallel. I’d read before that I’d need Audio 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 of code into the applicationDidFinishLaunching method of my application delegate (see below). Sputnik Games’ blog doesn’t seem to support comments, so I thought I’d buy his latest game, Aerolite, and write this post to show my appreciation. Thanks Mark!

Add to applicationDidFinishLaunching to enable music & sound effects at the same time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
      }
  }
}

Comments