【发布时间】:2019-07-25 14:13:33
【问题描述】:
AVAudioPlayer 在后台播放音频,用户通过耳机控件或锁定屏幕控件将其暂停。
如果用户在 30 秒内执行此操作并尝试恢复 - 一切正常。
如果用户尝试在 30 秒内恢复它 - 音频开始播放,但一秒后来自 AVAudioSessionDelegate 的 audioSessionInterruptionNotification 被触发,AVAudioSessionInterruptionWasSuspendedKey YES。
之后,应用程序完全停止对远程控制事件做出反应。正在触发事件,但 AVAudioPlayer 不会对任何命令做出反应。事实上,[[self audioplayer] isAudioPlaying] 在播放继续时返回 NO。
如果我尝试按以下方式处理它 - 它会有所帮助(所以我将 AVAudioSession 设置为不活动,然后 playHelper 方法将其激活并播放音频),但是有一个小故障,因为通知在它开始播放后被触发.
- (void)audioSessionInterruptionNotification:(NSNotification *)interruption {
UInt8 interruptionType = [[interruption.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] intValue];
NSLog(@"Session interrupted > --- %s ---\n", interruptionType == AVAudioSessionInterruptionTypeBegan ? "Begin Interruption" : "End Interruption");
NSDictionary *notificationUserInfo = [interruption userInfo];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
if ([notificationUserInfo valueForKey:AVAudioSessionInterruptionWasSuspendedKey]) {
[[AVAudioSession sharedInstance] setActive:false error:nil];
[self playHelper];
} else {
[self interruptionStarted];
}
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
[self interruptionEnded:(NSUInteger)[notificationUserInfo objectForKey:AVAudioSessionInterruptionOptionKey]];
}
}
请告知可能是什么原因造成的。
【问题讨论】:
标签: ios objective-c avaudioplayer avaudiosession