iOS AVAudioSession中断通知无法按预期工作 [英] iOS AVAudioSession interruption notification not working as expected

查看:1499
本文介绍了iOS AVAudioSession中断通知无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的 AVAudioRecorder 何时无法访问(例如音乐开始播放时)。

I want to know when my AVAudioRecorder is inaccessible (e.g when music starts playing).

As < iOS 9将弃用code> audioRecorderEndInterruption 我专注于 AVAudioSession 的中断通知(但两者都没有按预期工作)。

As audioRecorderEndInterruption will be deprecated with iOS 9 I am focusing on AVAudioSession's interruption notification (but neither is working as expected).

问题是,如果应用程序在中断发生时仍然在前台,则永远不会调用中断通知。

The issue is that the interruption notification is never called if the app was and remains in the foreground when the interruption occurs.

例如:用户启动和停止播放音乐而不将应用程序移动到后台。

E.g: The user starts and stops playing music without moving the application into the background.

要检测我正在使用的任何中断:

To detect any interruptions I am using:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
...
- (void)audioSessionWasInterrupted:(NSNotification *)notification {
    if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
        NSLog(@"Interruption notification");

        if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
            NSLog(@"InterruptionTypeBegan");
        } else {
            NSLog(@"InterruptionTypeEnded");
        }
    }
}

我得到 InterruptionTypeBegan 如预期的那样,但是如果应用程序仍在前台,则不会调用 InterruptionTypeEnded (意味着它不会被调用,直到应用程序放在后台并返回到前台。)

I get InterruptionTypeBegan as expected, but InterruptionTypeEnded isn't called if the app is still in the foreground (meaning it won't be called until the app is placed in the background and back into the foreground).

如果中断,我如何收到 InterruptionTypeEnded 通知当应用程序在前台时发生?

How may I receive InterruptionTypeEnded notification when the interruption occurs while the app is in the foreground?

推荐答案

这是一个影响使用 AV的任何应用程序的普遍问题框架组件(本机iOS应用也是如此)。

This is a widespread problem affecting any app using AV framework components (the same goes for native iOS apps).

的文档音频中断的主题, InterruptionT应该在上述场景中应用ypeEnded


如果用户解除中断......系统调用你的回调方法,表明中断已经结束。

If the user dismisses the interruption ... the system invokes your callback method, indicating that the interruption has ended.

但是,它还声明 InterruptionTypeEnded 可能根本不会被调用:

However, it also states that the InterruptionTypeEnded might not be called at all:


无法保证开始中断会导致结束中断。

There is no guarantee that a begin interruption will have an end interruption.

因此,在上述方案中需要采用不同的方法。

Therefore, a different approach is needed in the scenario mentioned.

在处理音乐中断时,问题不会持续很长时间。 iOS 9有效地防止在调用应用程序的音频处理程序时使用外部音频源。

When it comes to handling music interruptions, the issue won't be around for long. iOS 9 effectively prevents outside audio sources to be used while the app's audio handler is invoked.

处理媒体中断问题的一种方法可能是收听 MPMusicPlayerController playbackState ,如此stackoverflow问题所示:检测音乐是否在播放?

A way to handle the exact issue of media interruption could be to listen to MPMusicPlayerController's playbackState, as shown in this stackoverflow question: Detecting if music is playing?.

处理中断问题的更直接方法是:

A more direct way to handle the issue of interruptions would be to either:

通过在<$ c时重新调用音频组件来完全阻止音频中断$ c> InterruptionTypeBegan 。

或者通过提供外部媒体源中断音频会话的UI指示(例如显示非活动麦克风) )。

Or by giving a UI indication that an outside media source has interrupted the audio session (for example showing an inactive microphone).

希望会为这个问题找到更好的解决方案,但与此同时这应该会给你一些选项解决中断问题。

Hopefully  will come up with a better solution to the problem, but in the meantime this should give you some options to solve the interruption issue.

这篇关于iOS AVAudioSession中断通知无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆