所以我有一个音乐应用程序,它使用 AVAudioSession 允许它在后台播放。我用这个电话:
[audioSession setActive:YES
withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
error:nil];
我现在的问题是如果我转到另一个应用程序并且它窃取了 Audio Session (因此现在停止从我的应用程序播放音乐并播放其他东西),然后我回到我的应用程序,无论我做什么来重置我的 Audio Session 或我的音频单元,我的应用程序的声音消失了。
有人知道怎么办吗?
最佳答案
因此在注册 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 resuming after incoming call
关于ios - 应用程序重新激活后 AVAudioSession 无法播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196533/