我正在开发音频流媒体应用,而且我遇到了中断问题。 我已经读过我应该使用AVAudioSessionInterruptionNotification,但我的问题是它只在中断开始时才被调用,而不是在它结束时被调用。 这是我的代码。
static dispatch_once_t pred;
static Player *sharedAudioPlayer = nil;
dispatch_once(&pred, ^
{
sharedAudioPlayer = [[self alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer selector:@selector(playerItemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer
selector:@selector(audioSessionInterrupted:)
name:AVAudioSessionInterruptionNotification
object:audioSession];
});
然后
- (void)audioSessionInterrupted:(NSNotification*)notification {
NSDictionary *interruptionDictionary = [notification userInfo];
NSNumber *interruptionType = (NSNumber *)[interruptionDictionary valueForKey:AVAudioSessionInterruptionTypeKey];
if ([interruptionType intValue] == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Interruption started");
if (self.delegate && [self.delegate respondsToSelector:@selector(playerWasPaused)]) {
[self.delegate playerWasPaused];
}
} else if ([interruptionType intValue] == AVAudioSessionInterruptionTypeEnded){
NSLog(@"Interruption ended");
[player play];
} else {
NSLog(@"Something else happened");
}
当一个电话开始时,我会打印两次"中断开始"但是当它结束时我什么也得不到。我甚至试图检查是否有其他内容发送到通知但仍然没有。 有什么想法吗?