延时后播放音频文件

4

我有一个音频文件,我想在用户看到一些动画播放后延迟20秒开始播放。你知道我该怎么做吗?我有5个动画播放,之后我想让音频文件开始播放。整个过程将不断循环,直到用户退出应用程序。

这是我播放音频文件的代码。如果按下按钮或在viewDidLoad上播放,它可以播放,这很好。

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

AudioServicesPlaySystemSound(audioID);

感谢您的帮助。
2个回答

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);
}

2
你也可以使用:
```[self performSelector: @selector(playSound:) withObject: audioURL afterDelay: 20.0f];```

当然,您可以选择将任何您想要的内容作为参数传递,只要它可以转换为“id”。 - jv42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接