如果已经在玩就停止
Stop if already playing
我需要一些帮助来实现 .playing() 方法,因为当我的 playSound 函数被调用时,我想停止当前正在播放的任何东西,并播放新请求的声音,这是我的例子,它没有停止每个调用时间:
function playSound(audio) {
var playing;
var sound = new Howl({
src: ['assets/sound/voice/' + audio],
autoplay: false,
volume: 0.9,
onplay: function() {
playing = true;
},
onend: function() {
playing = false;
}
});
if(playing) {
sound.stop();
} else {
sound.play();
}
}
最终的解决方案是为 "sound" 创建一个全局变量:
var sound = null;
function playSound(audio) {
//check if sound is null, if not stop previous sound and unload it
if (sound != null) {
sound.stop();
sound.unload();
sound = null;
}
sound = new Howl({
src: ['assets/sound/voice/' + audio]
});
sound.play();
}
我需要一些帮助来实现 .playing() 方法,因为当我的 playSound 函数被调用时,我想停止当前正在播放的任何东西,并播放新请求的声音,这是我的例子,它没有停止每个调用时间:
function playSound(audio) {
var playing;
var sound = new Howl({
src: ['assets/sound/voice/' + audio],
autoplay: false,
volume: 0.9,
onplay: function() {
playing = true;
},
onend: function() {
playing = false;
}
});
if(playing) {
sound.stop();
} else {
sound.play();
}
}
最终的解决方案是为 "sound" 创建一个全局变量:
var sound = null;
function playSound(audio) {
//check if sound is null, if not stop previous sound and unload it
if (sound != null) {
sound.stop();
sound.unload();
sound = null;
}
sound = new Howl({
src: ['assets/sound/voice/' + audio]
});
sound.play();
}