监听耳机插孔事件

来源:互联网 发布:日程表 知乎 编辑:程序博客网 时间:2024/06/09 19:48

苹果设备的耳机插孔除了可以插入耳塞听音乐之外,还可以插入其他的的三方设备,比如刷卡器的拉卡拉app应用等。

这有一个简单的demo,用来监听耳机插孔插入,拔出,以及播放歌曲是的控制。

不过app和设备之间的通讯还没有解决,哪位大神解决了求分享!微笑


1.AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSError *error = nil;
    AVAudioSession *audionSession = [AVAudioSession sharedInstance];
    [audionSession setCategory:AVAudioSessionCategoryPlayback error:&error];
    [audionSession setActive:YES error:&error];


    [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
    AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     (__bridge void *)(self));


    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; // 这一句很重要
    return YES;
}


//触发的监听事件
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize,const void *inPropertyValue ) {
    
    NSString *msg=@"耳机插孔已拔出";
//    if (inPropertyID != kAudioSessionProperty_AudioRouteChange)return;
//    else{
        CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;
        CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
        
        SInt32 routeChangeReason;
        CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
        if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
            
            msg=@"耳机插孔已拔出";
        } else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {
            msg=@"耳机插孔已插入";
        }
//    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@""
//                                                 message:msg
//                                                delegate:nil
//                                       cancelButtonTitle:@"确定"
//                                       otherButtonTitles:nil, nil];
//    [alert show];
    
    NSLog(@"msg :%@",msg);
    
//    }
}



2.UIViewController

- (void)_playBt:(UIButton *)sender {
    
    NSLog(@"playVoice");
    NSString *path=[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],
                    @"ring.mp3"];
    if (!player) {
        NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    player.numberOfLoops=-1;
    [player setVolume:0];
    [player play]; //播放
}

- (void)viewDidLoad {
   [self _playBt:nil];
}



// 监听耳机插孔事件

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:// 103
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                break;
            case UIEventSubtypeRemoteControlNextTrack:// 104
                break;
            default:
                break;
        }
    }
}


0 0