通话后,后台音频不会恢复

car*_*los 22 audio background ios

我的背景音频几乎一直都很好.屏幕锁定或静音开启.但是当用户在后台运行应用程序并且它接收到呼叫时,即使用户没有应答呼叫,在中断结束后也不会恢复背景音频.

音乐应用程序如果被中断,则会正确恢复背景音频.

我是否遗漏了一些属性,或者我是否需要进行回调或设置后台任务以在中断后继续执行后台音频执行?或者这是我们做不到的事情?

Mic*_*ers 22

当应用程序在后台时,我们将在出站呼叫和入站呼叫后恢复音频.

我们使用AVAudioPlayer播放音频并收听AVAudioSessionInterruptionNotification.Apple会在中断时自动暂停AVAudioPlayer,当您在收到中断后告诉它恢复时,Apple会再次激活您的会话.如果您正在使用其他类型的音频技术,请参阅处理音频中断的表3-2 .

订阅通知:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

处理通知:

- (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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它在应用程序在前台播放音频时工作。如果应用程序是后台,则不会恢复。但是如果我来到前台然后工作。@Michale Myers 你能提出建议吗? (2认同)

Ton*_*ion 5

根据我的经验,我发现在尝试重新打开音频设备之前我必须"等待一两秒钟".我认为在操作系统从通话切换回您的应用后,手机应用程序仍在关机.

在知道您的音频会话已经停止后返回前台时会出现类似的情况:

            dispatch_time_t restartTime = dispatch_time(DISPATCH_TIME_NOW, 
                                                      1.5LL * NSEC_PER_SEC);

            dispatch_after(restartTime, dispatch_get_global_queue(0, 0), ^{ 
                [audioSystem restart]; 
            });
Run Code Online (Sandbox Code Playgroud)