2010-08-13 5 views
2

나는 아이폰과 목표 C로 새로운데, 지금까지 몇 가지 작은 예제를 코딩 할 수 있었다.소리를 내고, 연주를 끝내고 (iPhone) 계속하기를 기다린다.

샘플이 재생이 끝나면 나는 사운드를 재생하고, 코드의 나머지 부분을 계속하고 싶습니다 예 :

printf("hello"); 
playASound["file.wav"]; 
printf("world"); 

사실은 내가 받고 있어요 : 인쇄 인사에서 파일 및 인쇄 세상을 연주 같은 시간에 하지만 내가 원하는 건 : 안녕하세요, 파일을 재생, 인쇄 세계 ... 그럼, 어떻게해야합니까?

감사합니다.

btw입니다.

-(void) playASound: (NSString *) file { 

    //Get the filename of the sound file: 
    NSString *path = [NSString stringWithFormat:@"%@/%@", 
         [[NSBundle mainBundle] resourcePath], 
         file]; 

    SystemSoundID soundID; 
    //Get a URL for the sound file 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    //play the file 
    AudioServicesPlaySystemSound(soundID); 
} 

답변

10

documentation에서 : : 여기에 playASound 코드는

토론이 기능은 짧은 소리 (30 초 지속 시간 이하)한다. 사운드는 을 몇 초 동안 재생할 수 있으므로이 함수는 이 비동기 적으로 실행됩니다. 소리가 들릴 때를 알기 위해서는 AudioServicesAddSystemSoundCompletion 함수를 호출하여 콜백 함수를 등록하십시오.

그래서 당신은 두 조각으로 함수를 중단해야합니다 : PlayASound 및 인쇄 "안녕하세요"를 호출하는 기능, 사운드가 재생 및 인쇄 "세계"를 완료 할 때 called by the system 수있는 기능입니다.

// Change PlayASound to return the SystemSoundID it created 
-(SystemSoundID) playASound: (NSString *) file { 

    //Get the filename of the sound file: 
    NSString *path = [NSString stringWithFormat:@"%@/%@", 
         [[NSBundle mainBundle] resourcePath], 
         file]; 

    SystemSoundID soundID; 
    //Get a URL for the sound file 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    //play the file 
    AudioServicesPlaySystemSound(soundID); 
    return soundID; 
} 

-(void)startSound 
{ 
    printf("Hello"); 
    SystemSoundID id = [self playASound:@"file.wav"]; 
    AudioServicesAddSystemSoundCompletion (
     id, 
     NULL, 
     NULL, 
     endSound, 
     NULL 
    ); 
} 

void endSound (
    SystemSoundID ssID, 
    void   *clientData 
) 
{ 
    printf("world\n"); 
} 

docs for AudioServicesAddSystemSoundCompletionAudioServicesSystemSoundCompletionProc도 참조하십시오.

+0

+1이 또한 저에게 도움이됩니다 –

+0

감사합니다 Nicholas – subzero

+0

"경로"가 자동으로 해제 되나요? –

관련 문제