是否可以检测 iPod Touch/iPhone 是否连接了任何耳机或其他配件?
我正在构建一个需要麦克风的应用程序,并且需要通过底座连接或使用耳机端口(例如 Apple 的嵌入式耳机/麦克风配件)知道“iSomething”是否已连接。
是否可以检测 iPod Touch/iPhone 是否连接了任何耳机或其他配件?
我正在构建一个需要麦克风的应用程序,并且需要通过底座连接或使用耳机端口(例如 Apple 的嵌入式耳机/麦克风配件)知道“iSomething”是否已连接。
终于找到了——初始化Audio Session对象后——AudioSessionInitialize()——可以调用AudioSessionGetProperty,得到kAudioSessionProperty_AudioInputAvailable的值。
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 propertySize, micConnected;
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
[self updateMicStatus:micConnected]; // user-created method
根据音频会话服务的文档,应该使用它而不是使用设备模型(iPhone 与 iPod Touch)来确定音频输入是否可用。您还可以设置一个回调函数,通过 AudioSessionAddPropertyListener() 来监控此属性的更改。
尚不确定此属性是否也适用于通过 Dock 连接器连接的设备,但它似乎适用于耳机插孔。
或者你可以使用:
if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
// your code here for no audio input available
}
在IOS 6 inputIsAvailable
中已弃用。将来我们需要使用inputAvailable
:
BOOL audioHWAvailable = audioSession.inputAvailable;
要确定设备是否具有内置麦克风,您可以通过[UIDevice currentDevice].model
查看它是 iPhone 还是第二代 iPod Touch。至于第三方麦克风插入底座连接器,这在当前的 2.2.1 SDK 中是不可能的,但它可能会在以后的版本中:)
这是解决方案,您可能喜欢它或对您有帮助。
在使用以下方法之前,请同时写下这两行
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.");
}
}