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