2012-05-21 44 views
2

ios設備上有交互式電影。當電影開始時(點擊),視頻開頭的人會要求你插上耳機,如果插上耳機,視頻應該會自動直接跳到故事中(直接進入視頻故事)。我該怎麼辦?以及如何編寫代碼?檢測耳機是否插入iOS設備

+0

http://i48.tinypic.com/1g3lgl.jpg 在PIC忽略模糊部分 – user1369476

+1

參見[此](http://stackoverflow.com/questions/3575463/detecting-if-headphones-被插入iPhone)鏈接。它會幫助你 –

+0

如何註冊AudoRoute更改? – user1369476

回答

3

首先,你必須爲AudioRoute更改註冊: -

AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, 
            audioRouteChangeListenerCallback, 
           self); 

在這裏,您可以描繪出的理由進行更改路線: -

CFDictionaryRef routeChangeDictionary = inPropertyValue; 

    CFNumberRef routeChangeReasonRef = 
    CFDictionaryGetValue (routeChangeDictionary, 
         CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); 

    SInt32 routeChangeReason; 

     CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); 

    if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
    { 
     // your statements for headset unplugged 

    } 
    if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) 
    { 
     // your statements for headset plugged        
    } 
+0

如果用戶將使用已插入的耳機來啓動(點按),該怎麼辦?這是否意味着默認設置會被設置?我的意思是,我可以寫[routeChangeReason == kAudioSessionRouteChangeReason_HeadsetUnavailable] ...? – user1369476

+0

routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable我已經給插入耳機聲明瞭這個聲明。 –

+0

謝謝,但我的意思是這個算法:如果最初拔掉插頭,然後在5秒後播放A播放B(因爲可能有人沒有耳機),如果耳機按上述問題插入,則直接播放B.如果耳機原先在錄音前插入在視頻上,然後直接播放B.如果在播放過程中拔掉插頭,則轉到外部揚聲器。 – user1369476

0

這可能是另一種方式:

CFStringRef newRoute; 
size = sizeof(CFStringRef); 
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route"); 
if (newRoute) 
{ 
    CFShow(newRoute); 
    if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in 
      { 
... 
      } 
    else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo){ 
    .... 
    } 
} 
0

首先檢查設備是否連接到任何耳機。

+(BOOL)isHeadsetPluggedIn { 

AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute]; 
for (AVAudioSessionPortDescription* desc in [route outputs]) { 
    if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones]) 
     return YES; 
} 
return NO; 
} 

然後基於bool值,編寫自己的條件。像下面的東西..

if (isHeadphonesConnected) { 
    //Write your own code here 
}else{ 

} 

此外,你可以註冊一個通知,如果你想知道當你在屏幕上的耳機是否被刪除。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(audioRoutingListenerCallback:) 
             name:AVAudioSessionRouteChangeNotification 
              object:nil]; 

- (void)audioRoutingListenerCallback:(NSNotification*)notification 
{ 
    NSDictionary *interuptionDict = notification.userInfo; 

    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; 

    switch (routeChangeReason) { 

     case AVAudioSessionRouteChangeReasonNewDeviceAvailable: 

      NSLog(@"Headphone/Line plugged in"); 

      /*Write your own condition.*/ 

      break; 

     case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: 

      NSLog(@"Headphone/Line was pulled."); 

      /*Write your own condition.*/     
      break; 

     case AVAudioSessionRouteChangeReasonCategoryChange: 
      // called at start - also when other audio wants to play 

      break; 
    } 
}