I tried to figure out what was actually going on for several weeks, and I have no idea why I canโt continue playing after the break, so maybe you guys know the answer. AudioSessionSetActive (TRUE) always returns โ! Catโ, which is kAudioSessionIncompatibleCategory when re-activated if my application is playing in the background and I enter another application. Although it works fine and continues to play if I caught an interrupt while in my application.
The source code actually has all the AudioSession and AudioQueue calls wrapped in macros that print OSStatus if that means an error, but I deleted it for better readability. In addition, [self pause] just toggles the pause, so basically it calls AudioQueueStart (audioQueue, NULL), but this does not work if AudioSession does not work.
Audio Initialization Code:
AudioSessionInitialize(NULL, NULL, _audioSessionInterruptionListener, self); UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, _audioSessionPropertyListener, self); AudioSessionSetActive(TRUE);
Interrupt Handler Code:
- (void)handleInterruptionChangeToState:(AudioQueuePropertyID)inInterruptionState { if(inInterruptionState == kAudioSessionBeginInterruption) { NSLog(@"+Interruption"); if(self.state == NX_STATE_PLAY) { [self pause]; AudioSessionSetActive(FALSE); isPausedByInterruption = YES; } } else if(inInterruptionState == kAudioSessionEndInterruption) { if(isPausedByInterruption) { AudioSessionSetActive(TRUE); [self pause]; isPausedByInterruption = FALSE; } NSLog(@"-Interruption"); } }
This streamer source code can be found here https://bitbucket.org/and/amaudiostreamer/src/122de41fe6c0/AMAudioStreamer/AMAudioStreamer/Classes/NxAudioStreamer.m if it somehow helps to solve the problem.
objective-c iphone audioqueue
Andy
source share