我终于用 ViewDidLoad 循环播放了音频,但我真的很难用 ViewDidDissapear 让它停止。我读过 this和 this加上这个论坛上的许多答案和问题。如果我使用 [theAudio stop],我会收到错误,并且大多数其他教程都会出现错误。我究竟做错了什么?
我已经导入了 AVFoundation/AVFoundation.h 框架并添加了 AVAudioPlayerDelegate
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"safariSFX" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
theAudio.numberOfLoops = -1;
[super viewDidLoad];
}
最佳答案
您需要将 AVAudioPlayer
实例存储为 @property。这样,当 ViewWillDisappear
事件发生时,您可以调用 [theAudio stop];
在上面的代码中,您创建了一个本地变量,因此没有任何方法来引用它稍后。
关于ios - 在 ViewDidDissapear 上停止音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487733/