AVPlayer耳机插拔

AVPlayer耳机插拔暂停播放。

//耳机插拔监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
- (void)audioRouteChanged:(NSNotification *)noti {
    NSDictionary *dict = noti.userInfo;
    NSInteger reason = [[dict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    switch (reason) {
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            
            break;
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
        {
            AVAudioSessionRouteDescription *routeDes = dict[AVAudioSessionRouteChangePreviousRouteKey];
            AVAudioSessionPortDescription *portDes = routeDes.outputs[0];
            NSString *portType = portDes.portType;
            //耳机接口
            if ([portType isEqualToString:AVAudioSessionPortHeadphones])
            {
                //如果当前正在播放,拔掉耳机,暂停播放
                if (_playerState == VHAVPlayerStatePlaying)
                {
                    [self pause];
                }
            }
        }
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange:
            // called at start - also when other audio wants to play
            
            break;
    }
}
发布了131 篇原创文章 · 获赞 9 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Morris_/article/details/100583760