我在react中有一个简单的函数来播放音频…
handlePlayMusic() {
this.audio = new Audio(music)
this.audio.play()
}
handleStopMusic() {
this.audio.pause()
}
当我播放新音乐,即Audio(music)
中music
的值改变时,播放新音乐,但先前播放的音乐继续播放。当我播放新音乐时,我希望它停止。
有什么帮助吗?
在播放之前停止
this.audio
的早期实例!
var obj = {
handlePlayMusic: function(music) {
try {
this.audio.pause();
this.audio.currentTime = 0;
} catch (e) {
console.log(e);
}
this.audio = new Audio(music);
this.audio.play()
},
handleStopMusic: function() {
this.audio.pause()
}
};
obj.handlePlayMusic('audio/audio.mp3');
小提琴演示(让音频先加载!)