MP3 播放和停止问题?
这段代码工作正常,直到我退出视图并尝试返回它。如果我这样做,停止按钮将不再起作用。离开视图后如何让它继续工作?
-(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完成后您需要释放音频播放器。
您还应该在代码中的
play
之前使用[audioPlayerprepareToPlay];
。You need to release the audio player when you are done with it.
You should also use
[audioPlayer prepareToPlay];
beforeplay
in your code.