iOS开发: 音频会话线路改变

2016-12-22  本文已影响95人  伯wen

线路改变

具体代码

// 会话线路改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil];
/**
 音频会话线路变化通知
 */
- (void)handleRouteChange:(NSNotification *)notification
{
    // 通知信息
    NSDictionary *info = notification.userInfo;
    // 获取线路信息
    AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
    // 当耳机拔出后触发
    if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
        // 自定义的一个播放器, 当耳机拔出后停止播放音乐
        [self.player pause];
    }
}
上一篇下一篇

猜你喜欢

热点阅读