iPhone Audio Sessions – iPodMusicPlayer and AVAudioPlayer

While working on a iPhone project recently, I was looking for a way of playing sound effects via AVAudioPlayer at the same time as music playback via iPodMusicPlayer and had been running into problems until I found this blog post from Sputnik Games (thanks guys!) which solved my problem. Add the following code to the application-didFinishLaunchingWithOptions method in your app delegate in order to solve the problem:

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
    }
  }
}
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)
This entry was posted in Objective C, iOS, iPhone and tagged , , , . Bookmark the permalink.
blog comments powered by Disqus