2011-08-03 24 views
3

我一直試圖讓它在現在工作一段時間。 我已經完成了他們在文檔中所說的一切,但仍然沒有任何東西。將外部附件連接到3.5毫米耳機插孔時無法獲得通知

這是我的應用程序委託,它註冊爲本地通知代碼:

- (void) registerForLocalNotifications { 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(_accessoryConnected:) 
              name:EAAccessoryDidConnectNotification 
              object:nil]; 


[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(_accessoryDisconnected:) 
              name:EAAccessoryDidDisconnectNotification 
              object:nil]; 

[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; } 

以上摘自的applicationDidFinishLaunching調用。

這裏的連接/斷開方法的代碼:

- (void) _accessoryConnected:(NSNotification *)notification { 
      NSLog(@"_accessoryConnected"); } 

- (void) _accessoryDisconnected:(NSNotification*)notification { 
NSLog(@"_accessoryDisconnected"); } 


-(void) accessoryDidDisconnect:(EAAccessory *) accessory { 
NSLog(@"accessoryDidDisconnect"); } 

試圖連接來與iPhone和一無所獲,同爲我的外部附件我要與應用程序整合的耳機。

請幫忙, 謝謝, Shaul。

+0

那麼,在做了一些更多的閱讀後,我發現耳機插孔在外部附件框架中不受支持。有沒有其他框架可以使用?請任何人嗎? –

回答

4

您應該爲此使用AudioSessionPropertyListener。 EAAccessory通知用於連接到30引腳端口的硬件。 在viewDidLoad中在ViewDidUnLoad

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil); 

添加此偵聽並刪除它添加在視圖控制器使用下列方法。

BOOL isHeadsetPluggedIn() { 
    UInt32 routeSize = sizeof (CFStringRef); 
    CFStringRef route; 

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute, 
               &routeSize, 
               &route 
              );  
    NSLog(@"%@", route); 
    return (!error && (route != NULL) && ([(NSString*)route rangeOfString:@"Head"].location != NSNotFound)); 
} 

void audioSessionPropertyListener(void* inClientData, AudioSessionPropertyID inID, 
            UInt32 inDataSize, const void* inData) { 
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 

    // Determines the reason for the route change, to ensure that it is not 
    //  because of a category change. 
    CFDictionaryRef routeChangeDictionary = inData;  
    CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); 

    SInt32 routeChangeReason;  
    CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); 

    // "Old device unavailable" indicates that a headset was unplugged, or that the 
    // device was removed from a dock connector that supports audio output. 
    if (routeChangeReason != kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
     return; 

    if (!isHeadsetPluggedIn()) 
    { 
     AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 
    } 
    else 
    { 
     UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
     AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride); 
    }  
} 

請注意,我很久以前從某處獲得此代碼,並且它對我有用。現在不能歸因於我的來源,因爲我不知道我從哪裏得到它。

+0

試圖用這個C代碼進行管理,但是從我觸摸C開始已經有好幾年了。看到Objective-C中有一種叫做AVAudioSession的替代方案。你認爲我可以這樣做嗎? http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008240 –

+0

好吧,我設法讓你發佈的代碼工作,但只有在應用程序啓動之前耳機未連接到設備時才能使用。任何想法是爲什麼? –

+0

也在viewDidLoad上調用isHeadsetPluggedIn。上述代碼僅在收到音頻路由更改通知時調用它。 – Mugunth