staticvoid ASAudioSessionInterruptionListener(void *inClientData,UInt32 inInterruptionState)
{
AudioStreamer* streamer = (AudioStreamer *)inClientData;
[streamerhandleInterruptionChangeToState:inInterruptionState];
}
我们可以换一种实现方式:
AudioSessionInitialize (
NULL, // 'NULL' to use the default (main) run loop
NULL, // 'NULL' to use the default run loop mode
ASAudioSessionInterruptionListener, // a reference to your interruption callback
self // data to pass to your interruption listener callback
);
AVAudioSession *mySession = [AVAudioSessionsharedInstance];
// Specify that this object is the delegate of the audio session, so that
// this object's endInterruption method will be invoked when needed.
[mySession setDelegate:self];
且实现以下方法:
- (void) beginInterruption {
if ([self isPlaying]) {
[selfpause];
pausedByInterruption =YES;
}
}
NSError *activationError =nil;
- (void) endInterruption {
AudioSessionSetActive(true );
if ([selfisPaused] &&pausedByInterruption) {
[selfpause]; // this is actually resume
pausedByInterruption =NO; // this is redundant
}
}