检测iPhone/iPod Touch附件
是否可以检测iPod Touch/iPhone是否连接了耳机或其他附件检测iPhone/iPod Touch附件,iphone,objective-c,ipod-touch,Iphone,Objective C,Ipod Touch,是否可以检测iPod Touch/iPhone是否连接了耳机或其他附件 我正在构建一个需要麦克风的应用程序,并且需要知道“iSomething”是否有一个连接,或者通过dock连接,或者使用耳机端口,比如苹果的内嵌式耳机/麦克风附件。要确定设备是否有内置麦克风,您可以通过[UIDevice currentDevice].model查看是iPhone还是第二代iPod Touch。就插入dock连接器的第三方麦克风而言,这在当前的2.2.1 SDK中是不可能的,但可能在更高版本中:)最终找到了它-
我正在构建一个需要麦克风的应用程序,并且需要知道“iSomething”是否有一个连接,或者通过dock连接,或者使用耳机端口,比如苹果的内嵌式耳机/麦克风附件。要确定设备是否有内置麦克风,您可以通过
[UIDevice currentDevice].model
查看是iPhone还是第二代iPod Touch。就插入dock连接器的第三方麦克风而言,这在当前的2.2.1 SDK中是不可能的,但可能在更高版本中:)最终找到了它-初始化音频会话对象后,-AudioSessionInitialize()-您可以调用AudioSessionGetProperty,并获取kAudioSessionProperty\u 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 connector连接的设备,但它似乎适用于耳机插孔。或者您可以使用:
if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
// your code here for no audio input available
}
这是解决方案,你可能会喜欢,也可能对你有帮助 在使用下面的方法之前,请同时写下这两行
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.");
}
}
在IOS 6中,
inputIsAvailable
已被弃用。将来我们需要使用可输入的:
BOOL audioHWAvailable = audioSession.inputAvailable;
最好从设备类型中分离出来测试功能支持。由于某些原因,这对我不起作用。在未连接耳机的iPod Touch第二代上,返回TRUE…如果需要测试音频录制,此布尔值将允许您在不停止现有音频播放的情况下检查音频录制的可用性。