2010-10-08 79 views
4

我有,我想開始在20秒延遲播放音頻文件後,用戶已經看到了一些動畫播放等後...播放音頻文件的時間延遲

有誰知道我怎麼可能去做這個?我有5個動畫,播放後,我想音頻文件開始。整個事情將繼續騎自行車,直到用戶退出應用程序。

這是我播放音頻文件的代碼。如果按下按鈕或viewDidLoad,我可以播放它,這可以正常工作。

NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"]; 
CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath]; 
AudioServicesCreateSystemSoundID(audioURL, &audioID); 

AudioServicesPlaySystemSound(audioID); 

感謝您的幫助

回答

3

如果你知道它始終將是整整20秒鐘,然後你可以使用一個NSTimer調用啓動音頻的方法:

[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(startPlaying) userInfo:nil repeats:NO]; 


-(void)startPlaying { 

    NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"]; 
    CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath]; 
    AudioServicesCreateSystemSoundID(audioURL, &audioID); 
    AudioServicesPlaySystemSound(audioID); 
} 
+0

應該做的伎倆。非常感謝。 – hanumanDev 2010-10-08 14:56:22

2

你可以也使用

[self performSelector: @selector(playSound:) withObject: audioURL afterDelay: 20.0f];

+0

當然你可以選擇傳遞任何你想要的參數,只要它可以被轉換爲'id'。 – jv42 2010-10-08 14:52:16

+0

很棒。非常感謝。 – hanumanDev 2010-10-08 14:56:49