耳機線控

耳機的拔插

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{

//在這里我們就可以實現(xiàn)對音量鍵進行監(jiān)聽,完成響應的操作。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;

}

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容