我在暫停正在播放的mp3時遇到問題。使用JavaScript播放/暫停MP3問題
例如,如果我運行如下函數:playSong(drake);
它會從我的if語句的第一部分開始運行代碼,它將播放「drake」對象中的MP3並設置songValue = 2 問題在於,如果我第二次運行它,但它不會暫停歌曲,但是我的console.log顯示在控制檯中,所以當我第二次單擊它時它肯定會運行if語句的第二部分,但它不會「由於某些原因暫停歌曲。
//object with mp3 audio
var drake = {
value: 3,
name: 'Energy',
artist: 'Drake',
audio: 'energy.mp3', //
img: '<img style="width: 50%; margin-right: 25%; margin-left: 25%; margin-top: 10%;" src="http://www.getrichrapping.com/wp-content/uploads/2015/08/Drake-Energy.jpg">'
};
songValue = 1;
// plays and SHOULD pause a Song
function playSong(object) {
var Song = new Audio(object.audio);
//plays the song
if (songValue == 1) {
Song.play();
songValue = 2;
// SHOULD pause the song when the functions is runned the second time
} else if (songValue == 2) {
console.log("Is it working ?"); // String to test if the "else if" gets runned
Song.pause();
songValue = 1;
}
};
凡songValue被定義? object.value? –
哦,對不起,我忘了補充,但我剛剛更新了它 –