2012-05-21 3 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 초 후에 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; 
    } 
} 
관련 문제