2012-08-15 3 views
2

iOS 응용 프로그램에서 몇 가지 다른 작업을 수행하기 전에 3 초 정도의 짧은 소리 (예 : 삐 소리)를 재생해야합니다.다른 작업을 수행하기 전에 iOS가 소리를냅니다.

사용자가 버튼을 클릭 경고음이 간단한 비프 음이 방법의 나머지 부분이 실행 ... AudioServicesPlaySystemSound를 사용 (재생 ... 내가 수없는 것

다음과 같이

유스 케이스입니다. 톤이 재생하는 동안 내 방법을 차단하는 방법을 알아

나는 다음 시도했다 :.

[self performSelector:@selector(playConfirmationBeep) onThread:[NSThread currentThread] withObject:nil waitUntilDone:YES]; 

그러나 톤 동기 WH 재생 나머지 방법은 수행됩니다.

위의 호출에서 나는 무엇이 누락 되었습니까?

답변

2

AudioServicesPlaySystemSound은 비동기 적이므로 차단할 수 없습니다. 당신이하고 싶은 것은 재생이 끝났을 때 오디오 서비스로 알려주는 것입니다. 너는 AudioServicesAddSystemSoundCompletion을 통해 할 수있다. 상황이 조금 못생긴 있도록

IT는 C 레벨 API,하지만 당신은 아마 비슷한 원하는 : 당신이 실제로 소리가 재생 및 가정 동안 UI를 차단하려면

// somewhere, a C function like... 
void audioServicesSystemSoundCompleted(SystemSoundID ssID, void *clientData) 
{ 
    [(MyClass *)clientData systemSoundCompleted:ssID]; 
} 

// meanwhile, in your class' init, probably... 
AudioServicesAddSystemSoundCompletion(
    soundIDAsYoullPassToAudioServicesPlaySystemSound, 
    NULL, // i.e. [NSRunloop mainRunLoop] 
    NULL, // i.e. NSDefaultRunLoopMode 
    audioServicesSystemSoundCompleted, 
    self); 

// in your dealloc, to avoid a dangling pointer: 
AudioServicesRemoveSystemSoundCompletion(
      soundIDAsYoullPassToAudioServicesPlaySystemSound); 

// somewhere in your class: 
- (void)systemSoundCompleted:(SystemSoundID)sound 
{ 
    if(sound == soundIDAsYoullPassToAudioServicesPlaySystemSound) 
    { 
     NSLog(@"time to do the next thing!"); 
    } 
} 

당신의 클래스입니다 뷰 컨트롤러를 사용하는 경우 관련 기간 동안 self.view.userInteractionDisable을 비활성화해야합니다. 분명히하고 싶지 않은 것은 주행 루프를 차단하는 것입니다. 저 메모리 경고와 같은 중요한 시스템 이벤트가 중단되어 잠재적으로 앱이 강제 종료 될 수 있습니다. 장치 회전과 같은 일을 계속하기를 원할 수도 있습니다.

관련 문제