问题描述
我的背景音频几乎一直都能正常工作.屏幕锁定,或静音开关打开.但是当用户在后台有应用程序,并且有来电时,即使用户不接听电话,中断结束后后台音频也不会恢复.
My background audio works fine almost all the time. Screen locked, or mute switch on. But when the user has the application in the background, and it receives a call, even if the user doesn't answer the call, the background audio does not resumes after the interruption ends.
如果音乐应用被中断,它会正确恢复背景音频.
The Music app properly resumes background audio if it was interrupted.
我是否缺少某些属性,还是需要回调或设置后台任务以在中断后继续执行后台音频?或者这是我们做不到的?
Am I missing some property or do I need to have a callback or set a background task to continue background audio execution after an interruption? Or this is something that we can't do?
推荐答案
当应用程序在后台时,我们在呼出和呼入后恢复音频.
We are resuming our audio after an outbound call and inbound call while the app is in the background.
我们使用 AVAudioPlayer 并收听 AVAudioSessionInterruptionNotification.Apple 会在中断时自动为您暂停 AVAudioPlayer,当您在收到中断结束后告诉它恢复时,Apple 将再次将您的会话设置为活动状态.请参阅 处理音频中断 如果您正在使用其他类型的音频技术,请参阅建议.
We play audio with AVAudioPlayer and listen to the AVAudioSessionInterruptionNotification. Apple automatically pauses the AVAudioPlayer for you on an interruption and when you tell it to resume after you receive the interruption is over, Apple will set your session active again. See Table 3-2 from Handling Audio Interruptions on recommendations if you are using other types of audio technologies.
订阅通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
处理通知:
- (void) onAudioSessionEvent: (NSNotification *) notification
{
//Check the type of notification, especially if you are sending multiple AVAudioSession events here
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification received!");
//Check to see if it was a Begin interruption
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"Interruption began!");
} else {
NSLog(@"Interruption ended!");
//Resume your audio
}
}
}
这篇关于通话后背景音频不恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!