2010-08-26 38 views
8

Có ai biết nếu bạn có thể phát hiện xem tai nghe có được cắm vào iPhone hay không và nếu chúng không tắt âm thanh từ ứng dụng của bạn.Phát hiện xem tai nghe đã được cắm vào iPhone

Tôi nghĩ rằng tôi có thể quản lý việc tắt âm thanh, nhưng phần phát hiện tôi chưa tìm thấy bất kỳ thứ gì.

Cảm ơn

+0

Vui lòng truy cập liên kết này nơi mà tôi đã trả lời .. http://stackoverflow.com/questions/667196/detecting-iphone-ipod-touch-accessories/7575623#7575623 –

Trả lời

4

http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html

Trong dự án này có một mã đoạn nơi nó dừng ghi âm nếu tai nghe là unpluged. Có lẽ bạn có thể sử dụng nó để đạt được kết quả của bạn.

Chúc may mắn!

(chỉnh sửa)

Bạn sẽ phải nghiên cứu tệp SpeakHereController.mm.
tôi tìm thấy mã này trong awakeFromNib phương pháp

// 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); 
+0

Cảm ơn, tôi sẽ phải nhìn kìa! :) –

4

Dưới đây là giải pháp, bạn có thể thích hay nó là hữu ích cho bạn.

Trước khi sử dụng dưới phương pháp xin vui lòng viết hai dòng này cũng

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."); 
     } 
} 
+3

Cập nhật iOS 5 gần đây không được chấp nhận 'kAudioSessionProperty_AudioRoute', sử dụng 'kAudioSessionProperty_AudioRouteDescription' thay vì – petershine

+0

Mã này gây ra lỗi cho tôi - Tôi tin rằng vì AudioSessionGetPropertySize không thể diễn giải được trước hết. Xem câu trả lời của tôi làm việc trên iOS6 dưới đây (hoặc hy vọng ở trên nếu nó được upvoted ;-) –

+0

AudioSessionGetProperty hiện không còn được dùng nữa! –

6

Với mã này, bạn có thể phát hiện những thay đổi giữa:

  • MicrophoneWired
  • Headphone
  • lineout
  • Loa

Detecting when an iOS Device connector was plugged/unplugged

Lưu ý: Kể từ iOS 5 là một phần của "audioRouteChangeListenerCallback (...)" hành vi bị phản đối nhưng bạn có thể cập nhật nó với:

// 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 

Greetings

3

Để thực hiện một kiểm tra một lần để xác định xem tai nghe đã được cắm chưa (thay vì thiết lập gọi lại khi chúng được rút phích cắm) Tôi đã tìm thấy các công trình sau trong iOS5 trở lên:

- (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; 
} 

Đối với những điểm giữ ở nhà, đó là một chuỗi trong từ điển trong một mảng trong từ điển.

+2

Không được chấp nhận trong iOS7 – jomafer

Các vấn đề liên quan