耳機(jī)的拔插
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");
}
}
}
耳機(jī)的音量+力九、音量-
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(volumeClicked:)name:@"AVSystemController_SystemVolumeDidChangeNotification"object:nil];
-(void)volumeClicked:(NSNotification*)aNotification{
//在這里我們就可以實(shí)現(xiàn)對(duì)音量鍵進(jìn)行監(jiān)聽(tīng)谱邪,完成響應(yīng)的操作。noti中也有一些相關(guān)的信息可以看看
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檢測(cè)耳機(jī)是否插入(單純的判斷開(kāi)始的時(shí)候,是否存在耳機(jī))
[selfisHeadsetPluggedIn];
- (BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSessionsharedInstance]currentRoute];
for(AVAudioSessionPortDescription* descin[routeoutputs]) {
if([[descportType]isEqualToString:AVAudioSessionPortHeadphones])
returnYES;
}
returnNO;
}
耳機(jī)中間按鈕
//后臺(tái)控制
[[UIApplicationsharedApplication]beginReceivingRemoteControlEvents];
- (void)remoteControlReceivedWithEvent:(UIEvent*)receivedEvent {
NSLog(@">>>>>會(huì)走嗎>>>>>");
if(receivedEvent.type==UIEventTypeRemoteControl) {
switch(receivedEvent.subtype) {
caseUIEventSubtypeRemoteControlPause:
//點(diǎn)擊了暫停
NSLog(@">>>>>點(diǎn)擊了暫停>>>>>");
break;
caseUIEventSubtypeRemoteControlNextTrack:
//點(diǎn)擊了下一首
NSLog(@">>>>>點(diǎn)擊了下一首>>>>>");
break;
caseUIEventSubtypeRemoteControlPreviousTrack:
//點(diǎn)擊了上一首
//此時(shí)需要更改歌曲信息
NSLog(@">>>>>點(diǎn)擊了上一首>>>>>");
break;
caseUIEventSubtypeRemoteControlPlay:
//點(diǎn)擊了播放
NSLog(@">>>>>點(diǎn)擊了播放>>>>>");
break;
default:
break;
}
}
}