所以我有一个音乐应用程序,它使用AVAudioSession来允许它在后台播放。我用这个电话:
[audioSession setActive:YES
withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
error:nil];
我现在的问题是,如果我转到另一个应用程序,它会窃取音频会话(因此现在停止从我的应用程序播放音乐并播放其他内容),然后我回到我的应用,无论我如何重置音频会话或音频单元,我的应用程序的声音都会消失。
有人知道该怎么办吗?
因此,在注册AVAudioSession通知后:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification
object:aSession];
你需要恢复/重新启动你需要在处理程序中重新启动你的播放器中断类型是AVAudioSessionInterruptionTypeEnded
:
- (void)handleAudioSessionInterruption:(NSNotification*)notification {
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
// • Audio has stopped, already inactive
// • Change state of UI, etc., to reflect non-playing state
} break;
case AVAudioSessionInterruptionTypeEnded:{
// • Make session active
// • Update user interface
// • AVAudioSessionInterruptionOptionShouldResume option
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
// Here you should continue playback.
[player play];
}
} break;
default:
break;
}
}
你可以在这里看到完整的解释:AVplayer在来电后恢复