美文网首页
耳机线控

耳机线控

作者: L_Glenn | 来源:发表于2017-05-15 11:04 被阅读47次

    耳机的拔插

    NSError*error;

    [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:&error];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(outputDeviceChanged:)name:AVAudioSessionMediaServicesWereResetNotificationobject:[AVAudioSessionsharedInstance]];

    - (void)outputDeviceChanged:(NSNotification*)aNotification

    {

    NSDictionary*userInfoDic = [aNotificationuserInfo];

    NSLog(@"info1 : %@", userInfoDic);

    AVAudioSessionRouteDescription*dic = [userInfoDicobjectForKey:@"AVAudioSessionRouteChangePreviousRouteKey"];

    for(AVAudioSessionPortDescription* descin[dicoutputs]) {

    NSLog(@"lalalalal=%@", [descportType]);

    if([[descportType]isEqualToString:AVAudioSessionPortHeadphones]) {

    NSLog(@"fuck it");

    }

    }

    }

    耳机的音量+、音量-

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(volumeClicked:)name:@"AVSystemController_SystemVolumeDidChangeNotification"object:nil];

    -(void)volumeClicked:(NSNotification*)aNotification{

    //在这里我们就可以实现对音量键进行监听,完成响应的操作。noti中也有一些相关的信息可以看看

    NSDictionary*userInfoDic = [aNotificationuserInfo];

    NSLog(@"info2 : %@", userInfoDic);

    }

    [[UIApplicationsharedApplication]beginReceivingRemoteControlEvents];

    去掉显示音量提示框

    AVAudioSession*audio = [AVAudioSessionsharedInstance];

    [audiosetActive:YESerror:nil];

    MPVolumeView*volumeView = [[MPVolumeViewalloc]initWithFrame:CGRectMake(100,100,10,10)];

    volumeView.hidden=NO;

    [self.viewaddSubview:volumeView];

    //iOS检测耳机是否插入(单纯的判断开始的时候,是否存在耳机)

    [selfisHeadsetPluggedIn];

    - (BOOL)isHeadsetPluggedIn {

    AVAudioSessionRouteDescription* route = [[AVAudioSessionsharedInstance]currentRoute];

    for(AVAudioSessionPortDescription* descin[routeoutputs]) {

    if([[descportType]isEqualToString:AVAudioSessionPortHeadphones])

    returnYES;

    }

    returnNO;

    }

    耳机中间按钮

    //后台控制

    [[UIApplicationsharedApplication]beginReceivingRemoteControlEvents];

    - (void)remoteControlReceivedWithEvent:(UIEvent*)receivedEvent {

    NSLog(@">>>>>会走吗>>>>>");

    if(receivedEvent.type==UIEventTypeRemoteControl) {

    switch(receivedEvent.subtype) {

    caseUIEventSubtypeRemoteControlPause:

    //点击了暂停

    NSLog(@">>>>>点击了暂停>>>>>");

    break;

    caseUIEventSubtypeRemoteControlNextTrack:

    //点击了下一首

    NSLog(@">>>>>点击了下一首>>>>>");

    break;

    caseUIEventSubtypeRemoteControlPreviousTrack:

    //点击了上一首

    //此时需要更改歌曲信息

    NSLog(@">>>>>点击了上一首>>>>>");

    break;

    caseUIEventSubtypeRemoteControlPlay:

    //点击了播放

    NSLog(@">>>>>点击了播放>>>>>");

    break;

    default:

    break;

    }

    }

    }

    相关文章

      网友评论

          本文标题:耳机线控

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