来电后恢复的AVplayer [英] AVplayer resuming after incoming call

查看:20
本文介绍了来电后恢复的AVplayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AVPlayer 播放音乐.我的问题是来电后,播放器无法恢复.来电时如何处理?

I am using AVPlayer for music playback. My problem is that after an incoming call, the player won't resume. How do I handle this when an incoming call comes?

推荐答案

从 iOS 6 开始,你必须处理 AVAudioSessionInterruptionNotificationAVAudioSessionMediaServicesWereResetNotification,在此之前你必须使用委托方法.

Starting from iOS 6 you must handle AVAudioSessionInterruptionNotification and AVAudioSessionMediaServicesWereResetNotification, before this you had to use delegate methods.

首先,您应该调用 AVAudioSession 单例并将其配置为您想要的用途.

First you should call the AVAudioSession singleton and configure it for your desired use.

例如:

AVAudioSession *aSession = [AVAudioSession sharedInstance];
[aSession setCategory:AVAudioSessionCategoryPlayback
          withOptions:AVAudioSessionCategoryOptionAllowBluetooth 
                error:&error];
[aSession setMode:AVAudioSessionModeDefault error:&error];
[aSession setActive: YES error: &error];

那么你应该实现两个方法,用于 AVAudioSession 将调用的通知:

Then you should implement two methods, for the notifications which the AVAudioSession would call:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession];

第一个是由于来电、闹钟等而被调用的任何中断.

First one is for any interruption which would be called because of an incoming call, alarm clock, etc.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleMediaServicesReset)
                                             name:AVAudioSessionMediaServicesWereResetNotification
                                           object:aSession];

第二个如果媒体服务器因任何原因重置,您应该处理此通知以重新配置音频或进行任何内务处理.顺便说一下,通知字典不会包含任何对象.

The second one if the media server resets for any reason, you should handle this notification to reconfigure audio or do any housekeeping. By the way the notification dictionary won't contain any object.

这是一个处理播放中断的例子:

Here is an example for handling the playback interruption:

- (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;
    }
}

注意,当可选值为AVAudioSessionInterruptionOptionShouldResume

对于其他通知,您应该注意以下事项:

And for the other notification you should take care of the following:

- (void)handleMediaServicesReset {
// • No userInfo dictionary for this notification
// • Audio streaming objects are invalidated (zombies)
// • Handle this notification by fully reconfiguring audio
}

问候.

这篇关于来电后恢复的AVplayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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