美文网首页
监控耳机事件

监控耳机事件

作者: 哎呦我去叫什么呢 | 来源:发表于2016-12-08 10:29 被阅读18次

    -(void)whetherTheHeadsetInsert{
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    //创建单例对象并且使其设置为活跃状态.
    [[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;
      

      }
      }

    相关文章

      网友评论

          本文标题:监控耳机事件

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