2011-11-16 3 views
6

mute/silence 스위치가 Apple에서 iPhone의 상태를 감지하는 방법을 제공하지 않는다는 것을 알고 있습니다. iOS 5. 그러나 오디오 파일을 재생하고 런타임을 측정하여이를 감지하는 기술을 시도했습니다. 그러나 iPhone이 음소거 된 경우에도 오디오 파일은 계속 재생됩니다. 나는 audioPlayerDidFinishPlaying이 호출되기 전에 [AVAudioPlayer play]을 사용하고 시간을 계산하고 있습니다. 오디오 파일을 재생하는 트릭을 알고 있습니까? 전화가 음소거되면 즉시/즉시 완료됩니다.iOS 5에서 iPhone 음소거 스위치 감지하기

답변

4

업데이트 : 죄송합니다. 아래 게시 된 코드는 잘 작동하지만 iOS4를 사용하고 있습니다. iOS5의 경우이 대답을 통해 문제가 해결됩니다. - Detecting the iPhone's Ring/Silent/Mute switch using AVAudioPlayer not working?

이 코드는 필요한 것입니다. 글로벌 gAudioSessionInited var를 정의하십시오. 이 플래그는 뮤트 스위치가 켜져 있는지 여부를 알려줍니다.

// "Ambient" makes it respect the mute switch. Must call this once to init session 
if (!gAudioSessionInited) 
{ 
    AudioSessionInterruptionListener inInterruptionListener = NULL; 
    OSStatus error; 
    if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL))) 
     NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error); 
    else 
     gAudioSessionInited = YES; 
} 

SInt32 ambient = kAudioSessionCategory_AmbientSound; 
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient)) 
{ 
    NSLog(@"*** Error *** could not set Session property to ambient."); 
} 
+0

안녕. 그것은 ios에서 일하고 있습니까?. – Gopinath

+0

네 이것은 ios 용입니다. 어떤 버전이 당신에 대해 이야기하고 있습니까? –

+1

실수로 인해 미안합니다. 그것은 ios5 일하고 있습니까?. 미안해. – Gopinath

관련 문제