能否检测iPod Touch/iPhone是否连接了耳机或其他配件?
我正在开发一个需要麦克风的应用程序,需要知道"iSomething"是否已经连接了麦克风,无论是通过底座连接还是使用耳机插孔,例如使用苹果的内置耳机/麦克风配件。
能否检测iPod Touch/iPhone是否连接了耳机或其他配件?
我正在开发一个需要麦克风的应用程序,需要知道"iSomething"是否已经连接了麦克风,无论是通过底座连接还是使用耳机插孔,例如使用苹果的内置耳机/麦克风配件。
终于找到了 - 在初始化音频会话对象 Audio Session object 之后,使用 AudioSessionInitialize(),您可以调用 AudioSessionGetProperty 并获取 kAudioSessionProperty_AudioInputAvailable 属性的值。
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 propertySize, micConnected;
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
[self updateMicStatus:micConnected]; // user-created method
根据“音频会话服务”的文档,应该使用此属性来确定是否有可用的音频输入,而不是使用设备型号(iPhone vs. iPod Touch)。您还可以设置回调函数通过AudioSessionAddPropertyListener()来监控此属性的更改。
尚不确定此属性是否也适用于通过Dock连接器连接的设备,但它似乎适用于耳机插孔。或者您可以使用以下方法:
if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
// your code here for no audio input available
}
在 iOS 6 中,inputIsAvailable
已经被弃用。未来我们需要使用 inputAvailable
:
BOOL audioHWAvailable = audioSession.inputAvailable;
这里是解决方案,您可能会喜欢它或者对您有所帮助。
在使用下面的方法之前,请先写下以下两行。
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
- (void)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
//NSLog(@"Error >>>>>>>>>> :%@", error);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
NSString* routeStr = (NSString*)route;
NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];
if(headsetRange.location != NSNotFound) {
// Don't change the route if the headset is plugged in.
NSLog(@"headphone is plugged in ");
}
else if (receiverRange.location != NSNotFound) {
// Change to play on the speaker
NSLog(@"play on the speaker");
}
else {
NSLog(@"Unknown audio route.");
}
}
要确定设备是否有内置麦克风,您只需通过[UIDevice currentDevice].model
来查看它是iPhone还是第二代iPod Touch。至于插入到底座连接器的第三方麦克风,在当前的2.2.1 SDK中不可能实现,但在以后的版本中可能会有 :)