AVAudioPlayer重置当前播放的声音,并从开始玩它(AVAudioPlayer resett

2019-07-30 13:35发布

我使用AVAudioPlayer有,我想重置一个球员,如果它当前正在播放,并有再次发挥的问题。

我尝试没有运气如下:

声音播放一次,但然后我第二次选择它停止声音按钮,第三次再次启动的声音响了起来。

//Stop the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    [player stop];
    [player play];
} else {
    NSLog(@"playSound: %@", selectedSound);
    [player play];      
}

我已经使用player.currentTime = 0,这表明会重置播放器,没有工作,我也尝试重置currentTime的= 0,然后调用播放,没有工作也试过。

//Stop the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    player.currentTime = 0;
    [player play];
} else {
    NSLog(@"playSound: %@", selectedSound);
    [player play];      
}

Answer 1:

对于你的情况我想尝试以下

//Pause the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    [player pause];
}

player.currentTime = 0;
[player play];

如果你要立即再次播放声音,我会建议暂停而不是停止。 呼吁停止“停止播放,并撤消需要播放的设置。”



Answer 2:

斯威夫特例如:

player.currentTime = 0.0;


Answer 3:

如图所示,如果你想要的声音再次发挥,你应该避免与停止禁用AVAudioPlayer实例,但更确切地说,只是发出在那里打的位置复位。 所以,不要用stop()或暂停(),但作为Plumenator提到,只是发出.currentTime = 0。 所以你的情况,你会简单地做:

if (player.isPlaying) { 
   player.currentTime = 0
} else {
   player.play()
}


文章来源: AVAudioPlayer resetting currently playing sound and playing it from beginning