回答
http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html
在这个项目中有一个代码片段,如果耳机是unpluged它暂停录制。也许你可以用它来实现你的结果。
祝你好运!
(编辑)
你将不得不学习SpeakHereController.mm文件。
我发现这个代码在awakeFromNib
方法
// we do not want to allow recording if input is not available
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
btn_record.enabled = (inputAvailable) ? YES : NO;
// we also need to listen to see if input availability changes
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
谢谢,我会看看那个! :) – 2010-08-26 15:28:53
这里是解决方案,你可以喜欢它或者是对你有所帮助。
使用下面的方法之前,请写出这两个线路也
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 5更新已弃用'kAudioSessionProperty_AudioRoute',请使用'kAudioSessionProperty_AudioRouteDescription'代替 – petershine 2011-10-31 03:51:25
此代码对我造成了一个错误 - 我相信因为AudioSessionGetPropertySize莫名其妙地被首先调用。看到我的答案在iOS6下面工作(或希望如果它得到upvoted上面;-) – 2013-08-07 09:35:22
AudioSessionGetProperty现已被弃用! – 2015-04-27 16:10:20
有了这个代码,你可以检测之间的变化:
- MicrophoneWired
- 耳机
- 线路输出
- 音箱
Detecting when an iOS Device connector was plugged/unplugged
注意:由于 “audioRouteChangeListenerCallback(...)” 的iOS 5的部分特性已经被废弃,但你可以更新:
// kAudioSession_AudioRouteChangeKey_PreviousRouteDescription -> Previous route
// kAudioSession_AudioRouteChangeKey_CurrentRouteDescription -> Current route
CFDictionaryRef newRouteRef = CFDictionaryGetValue(routeChangeDictionary, kAudioSession_AudioRouteChangeKey_CurrentRouteDescription);
NSDictionary *newRouteDict = (NSDictionary *)newRouteRef;
// RouteDetailedDescription_Outputs -> Output
// RouteDetailedDescription_Outputs -> Input
NSArray * paths = [[newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] count] ? [newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] : [newRouteDict objectForKey: @"RouteDetailedDescription_Inputs"];
NSString * newRouteString = [[paths objectAtIndex: 0] objectForKey: @"RouteDetailedDescription_PortType"];
// newRouteString -> MicrophoneWired, Speaker, LineOut, Headphone
问候
要执行一次性检查以确定是否插入了耳机(而不是在拔下插头时设置回拨)我在iOS5及以上版本中发现以下作品:
- (BOOL) isAudioJackPlugged
{
// initialise the audio session - this should only be done once - so move this line to your AppDelegate
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 routeSize;
// oddly, without calling this method caused an error.
AudioSessionGetPropertySize(kAudioSessionProperty_AudioRouteDescription, &routeSize);
CFDictionaryRef desc; // this is the dictionary to contain descriptions
// make the call to get the audio description and populate the desc dictionary
AudioSessionGetProperty (kAudioSessionProperty_AudioRouteDescription, &routeSize, &desc);
// the dictionary contains 2 keys, for input and output. Get output array
CFArrayRef outputs = CFDictionaryGetValue(desc, kAudioSession_AudioRouteKey_Outputs);
// the output array contains 1 element - a dictionary
CFDictionaryRef dict = CFArrayGetValueAtIndex(outputs, 0);
// get the output description from the dictionary
CFStringRef output = CFDictionaryGetValue(dict, kAudioSession_AudioRouteKey_Type);
/**
These are the possible output types:
kAudioSessionOutputRoute_LineOut
kAudioSessionOutputRoute_Headphones
kAudioSessionOutputRoute_BluetoothHFP
kAudioSessionOutputRoute_BluetoothA2DP
kAudioSessionOutputRoute_BuiltInReceiver
kAudioSessionOutputRoute_BuiltInSpeaker
kAudioSessionOutputRoute_USBAudio
kAudioSessionOutputRoute_HDMI
kAudioSessionOutputRoute_AirPlay
*/
return CFStringCompare(output, kAudioSessionOutputRoute_Headphones, 0) == kCFCompareEqualTo;
}
对于那些在家中保持得分的人,这是字典中数组字典中的字符串。
在iOS7中已弃用 – jomafer 2014-08-27 10:11:06
- 1. 检测耳机是否插入iPhone/iPod
- 2. 检测耳机是否插入Java耳机插孔
- 3. 通过VBScript检测耳机是否插入或未插入
- 4. Android:检查是否插入了耳机
- 5. 检查耳机是否已插入
- 6. 如何检查耳机是否插入?
- 7. iPhone中的插孔/耳机检测
- 8. iPhone耳机点击检测
- 9. 检测耳机是否插入Android设备。
- 10. 检测耳机是否插入iOS设备
- 11. 您能否告诉耳机/耳机是否插入WP8
- 12. 检测耳机是否有麦克风
- 13. 检测耳机按钮点击iPhone SDK
- 14. 检查应用程序启动时是否插入了耳机
- 15. iPhone耳机插孔 - 读入数据?
- 16. Android:检测耳机以及耳机?
- 17. 如何检测WinRT中未插入的耳机插孔?
- 18. 检查耳机是否在Android手机
- 19. 检测按钮是否按下带有两个耳机的头戴式耳机?
- 20. Python脚本如何检测耳机/耳机是否连接到PC?
- 21. 使用耳机检测用户输入
- 22. AVAudioEngine在插入耳机或插入耳机时崩溃
- 23. 如果它检测到插入了耳机,会做些什么
- 24. 如何检测在Android 8.0上插入耳机的targetSDKVersion> = 26
- 25. 在wp7中检测插入/拔出耳机
- 26. 检测耳机未插入 - 单点拨号
- 27. 安卓耳机检测
- 28. 在Windows中检测耳机
- 29. 检测耳机按钮按
- 30. 检测麦克风/耳机?
请访问此链接,我已经回答了.. http://stackoverflow.com/questions/667196/detecting-iphone-ipod-touch-accessories/7575623#7575623 – 2011-09-27 21:06:26