AVAudioPlayer重置当前正在播放的声音并从开始播放

问题描述:

我在使用AVAudioPlayer时遇到了问题,我想重置播放器,如果它正在播放并再次播放它。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];  
} 

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

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

player.currentTime = 0; 
[player play]; 

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

+6

FWIW,我已经看到了刚刚设置currentTime的零就足够了。用这里描述的方法,我看到音频在连续几次之后拒绝播放。 – Plumenator 2011-06-13 09:09:57

斯威夫特例如:

player.currentTime = 0.0;