MP3 播放和停止问题?

发布于 2024-08-20 23:41:58 字数 435 浏览 9 评论 0原文

这段代码工作正常,直到我退出视图并尝试返回它。如果我这样做,停止按钮将不再起作用。离开视图后如何让它继续工作?

-(IBAction)musiconButtonClicked:(id)sender{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"NextRevIt" ofType:@"mp3"];
    self.audioPlayer = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer play];


}

-(IBAction)musicoffButtonClicked:(id)sender{

    [audioPlayer stop];

}

This code works fine until I exit the view and try to come back into it. If I do this, the stop button no longer works. How do I get it to still work after leaving the view?

-(IBAction)musiconButtonClicked:(id)sender{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"NextRevIt" ofType:@"mp3"];
    self.audioPlayer = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer play];


}

-(IBAction)musicoffButtonClicked:(id)sender{

    [audioPlayer stop];

}

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

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

发布评论

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

评论(1

巷子口的你 2024-08-27 23:41:58

完成后您需要释放音频播放器。

您还应该在代码中的 play 之前使用 [audioPlayerprepareToPlay];

You need to release the audio player when you are done with it.

You should also use [audioPlayer prepareToPlay]; before play in your code.

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