如果用户离开视图如何停止音频播放

发布于 2024-09-26 14:41:22 字数 614 浏览 0 评论 0原文

我有一个音频文件,当用户单击一个视图时会播放该音频文件。我的问题是,如果他们移动到另一个视图,音频会继续播放。如果用户不再处于该视图中,我希望音频停止。

这是在 viewDidUnload 方法中完成的吗?

这是 15 秒延迟后播放音频的代码:

-(void)startPlaying {

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


    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 plays when a user clicks on one view. my problem is that if they move to another view the audio continues to play. I would like the audio to stop if the user is no longer on that view.

is this done in the viewDidUnload method?

here's the code that plays the audio after a 15sec delay:

-(void)startPlaying {

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


    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 14:41:22
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // Stop Sound
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // Stop Sound
}
轻许诺言 2024-10-03 14:41:22

-(void)viewWillDisappear:(BOOL)动画{
[超级viewWillDisappear:动画];

AudioServicesDisposeSystemSoundID(audioID);

}

-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

AudioServicesDisposeSystemSoundID(audioID);

}

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