延迟一段时间后播放音频文件

发布于 2024-09-26 15:57:20 字数 488 浏览 3 评论 0原文

我有一个音频文件,我想在用户看到一些动画播放等后,以 20 秒的延迟开始播放......

有谁知道我会如何做到这一点?我有 5 个动画正在播放,之后我想要开始播放音频文件。整个过程将继续循环,直到用户退出应用程序。

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

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

AudioServicesPlaySystemSound(audioID);

感谢您的帮助

I have an audio file that I want to begin playing at a 20 second delay, after the user has seen some animations play etc...

Does anyone know how I might go about doing this? I have 5 animations that play, after which I would like audio file to begin. The whole thing would keep cycling until the user exits the app.

Here's the code I have that plays the audio file. I can get it to play if a button is pressed or on viewDidLoad, that works fine.

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

AudioServicesPlaySystemSound(audioID);

thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-10-03 15:57:20

如果您知道它总是正好 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);
}

If you know its always going to be exactly 20 seconds then you can use an NSTimer to call a method that starts the audio:

[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);
}
余生再见 2024-10-03 15:57:20

您还可以使用

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

You can also use

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文