且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

来电后恢复的AVplayer

更新时间:2023-02-02 16:10:02

从 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
}

问候.