回答
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. Android:檢查是否插入了耳機
- 3. iPhone中的插孔/耳機檢測
- 4. 檢測耳機是否插入Android設備。
- 5. iPhone耳機點擊檢測
- 6. 您能否告訴耳機/耳機是否插入WP8
- 7. iPhone耳機插孔 - 讀入數據?
- 8. 如何檢測WinRT中未插入的耳機插孔?
- 9. 檢查耳機是否在Android手機
- 10. Python腳本如何檢測耳機/耳機是否連接到PC?
- 11. AVAudioEngine在插入耳機或插入耳機時崩潰
- 12. 在wp7中檢測插入/拔出耳機
- 13. 檢測耳機未插入 - 單點撥號
- 14. 如何檢測在Android 8.0上插入耳機的targetSDKVersion> = 26
- 15. 在Windows中檢測耳機
- 16. 通過Ionic檢測耳機
- 17. 安卓耳機檢測
- 18. 檢測麥克風/耳機?
- 19. 如何檢測耳機是否連接到iPod touch G1?
- 20. 檢測是否連接了藍牙耳機
- 21. 檢測設備是否在Daydream VR耳機
- 22. JavaME:如何檢測耳機插件和插件?
- 23. 檢測硬件耳機機在Mac
- 24. Iphone禁用耳機耳機按鈕
- 25. 如何在開始服務時檢測BT耳機/耳機
- 26. 有線耳機插入和插出
- 27. 閱讀iPhone耳機插孔或藍牙(心率測量)
- 28. 如何檢測Galaxy S是否插入電視機?
- 29. 使用Java來檢測設備是否插入到計算機
- 30. [Android]耳機插入時只有耳機聲音
請訪問此鏈接,我已經回答了.. http://stackoverflow.com/questions/667196/detecting-iphone-ipod-touch-accessories/7575623#7575623 – 2011-09-27 21:06:26