【发布时间】:2014-08-05 12:18:00
【问题描述】:
我正在开发一个使用 AVAudioPlayer 的 Xcode 应用程序。
@property (nonatomic, strong) AVAudioPlayer *player;
点击图像时,我调用了一个播放音频文件的函数:
-(void)playMusic{
if(self.player.currentTime == 0.0){
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]] error:nil];
self.player.numberOfLoops = 0;
[self.player play];
}
}
在 viewWillDisappear 上我需要停止播放器,所以我打电话:
-(void)viewWillDisappear:(BOOL)animated{
[self.player stop];
}
但是上面的代码不起作用,并且音频没有停止播放。
谁能告诉我可能是什么问题?
【问题讨论】:
-
你有没有试过调用[self.player pause];?
-
另外你应该使用 _player 而不是 self.player
-
我试过 [self.player pause];但它不起作用。我也试过 [_player pause];
-
你检查过 viewWillDisappear 是否被调用
-
是的,它被调用了,我尝试使用 NSLog
标签: ios xcode avaudioplayer