耳机的拔插
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;
}
}
}