我使用 AVAudioPlayer 播放 10 秒的 wav 文件,效果很好。
现在我要在第 4 秒停止 wav 然后从
第一秒。
这是我试过的代码:
NSString *ahhhPath = [[NSBundle mainBundle] pathForResource:@"Ahhh" ofType:@"wav"];
AVAudioPlayer *ahhhhhSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL];
[ahhhhhSound stop];
[ahhhhhSound play];
我得到的是,wav 在第 4 秒停止,但是当我再次运行 [XXX play] 时,wav 继续播放第 5 秒而不是从头开始播放。
我怎么能做到这一点?任何帮助将不胜感激。
最佳答案
苹果AVAudioPlayer class reference说:
stop 方法不会将 currentTime 属性的值重置为 0。换句话说,如果您在播放过程中调用 stop 然后调用 play,则播放会从停止的点恢复。
所以你应该能够通过以下方式重新启动它:
[ahhhhhSound stop];
ahhhhhSound.currentTime = 0;
[ahhhhhSound play];
关于cocoa-touch - AVAudioPlayer 停止声音并从头开始播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3845906/