2010-08-26 9 views
8

Kulaklıkların iPhone'a takılıp takılmadığını algılayabiliyor olup olmadığını ve eğer yapmıyorsanız - uygulamanızdan gelen sesi devre dışı bırakıp bırakmadığını bilen var mı?Kulaklıkların iPhone'a takılı olup olmadığını algılama

Sanırım devre dışı bırakan sesi yönetebiliyordum, ancak henüz bir şey bulamadığım algılama parçası.

Teşekkür

+0

ben zaten cevap vermiş bu bağlantıyı .. http://stackoverflow.com/questions/667196/detecting-iphone-ipod-touch-accessories/7575623#7575623 –

cevap

4

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

Bu projede kulaklık unpluged ise o kayıt duraklar bir kod parçacığı vardır. Belki sonuca ulaşmak için kullanabilirsin.

İyi şanslar!

(düzenleme)

Sen SpeakHereController.mm dosyasını incelemek gerekir.
Ben awakeFromNib yöntemle İşte

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

Teşekkür, ben gerekecek ziyaret ediniz şuna bir bak! :) –

4

Bu kodu bulundu çözümdür, sizin gibi olabilir ya da size yardımcı olur.

yönteminin altında kullanmadan önce arasındaki değişiklikleri tespit edebilir Bu kod ile bu iki çizgi de

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

Son iOS 5 güncellemesi 'kAudioSessionProperty_AudioRoute' kullanımdan kaldırıldı, 'kAudioSessionProperty_AudioRouteDescription 'yerine – petershine

+0

kullanın Bu kod benim için bir hataya neden oldu - çünkü inanıyorum ki ilk olarak AudioSessionGetPropertySize çağrılmalıydı. Aşağıdaki iOS6'da çalışmamdaki cevabımı görün (ya da umarız yukarıdaysa; umarız ;-) –

+0

AudioSessionGetProperty artık kullanımdan kaldırıldı! –

3

bir gerçekleştirmek için

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

Selamlar Kulaklıkların takılı olup olmadığını belirlemek için bir defaya mahsus kontrol (takılı değilken bir geri çağırma ayarlamak yerine) Aşağıdakileri iOS5 ve sonraki sürümlerde buldum:

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

Evdeki puanı koruyanlar için, bu sözlükteki bir dizideki bir sözlükteki dizedir.

+2

iOS7'de kullanımdan kaldırıldı – jomafer

İlgili konular