之前 AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route); 的方法在 ios7之后被禁用了,现在有新的方法来检测iOS设备是否插入耳机:
1 -(BOOL)hasHeadset 2 { 3 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 4 5 AVAudioSessionRouteDescription *currentRoute = [audioSession currentRoute]; 6 7 for(AVAudioSessionPortDescription *output in currentRoute.outputs) 8 { 9 if([[output portType] isEqualToString:AVAudioSessionPortHeadphones]) 10 { 11 return YES; 12 } 13 14 } 15 16 return NO; 17 18 }