美文网首页iOS开发iOS 开发 iOS开发
iOS 监听耳机插入和拔出[检索]

iOS 监听耳机插入和拔出[检索]

作者: 2d899c5242bd | 来源:发表于2016-01-26 23:21 被阅读658次
    使用通知的方式,添加收到通知的回调方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:)   name:AVAudioSessionRouteChangeNotification object:nil];
    
    响应通知回调。
    - (void)audioRouteChangeListenerCallback:(NSNotification*)notification
    {
    NSDictionary *interuptionDict = notification.userInfo;
    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    switch (routeChangeReason) {
    case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
    NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
    NSLog(@"耳机插入");
    break;
    case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
    NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
    NSLog(@"耳机拔出,停止播放操作");
    break;
    case AVAudioSessionRouteChangeReasonCategoryChange:
    // called at start - also when other audio wants to play
    NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
    break;
    }
    }
    

    相关文章

      网友评论

        本文标题:iOS 监听耳机插入和拔出[检索]

        本文链接:https://www.haomeiwen.com/subject/ycddkttx.html