簡體   English   中英

我需要在點擊按鈕上播放下一首歌

[英]I need to play next song when click on on-click button

var song1 = $('#sound-1');
var song2 = $('#sound-2');

var audioArray = [song1, song2];
var i=0;
var lastPlayedFile = null;
$(".click").click(function(){
    if(lastPlayedFile !== null) {
        lastPlayedFile[0].currentTime = 0;
        lastPlayedFile.trigger('pause');
    }
    if (i< audioArray.length){
        lastPlayedFile = audioArray[i];
        audioArray[i].trigger('play');
        i++;
    } else if (i>=audioArray.length){
        i = 0;
        lastPlayedFile = audioArray[0];
        audioArray[i].trigger('play');
    };
});

該代碼對我不起作用,我正在使用firefox Web瀏覽器。 這段代碼有什么問題嗎?

  1. 請確保您使用的是正確<audio>和所示的標簽在這里 瀏覽器將選擇第一個可識別的源:

    <audio controls> // browser tries to fetch horse.ogg first <source src="horse.ogg" type="audio/ogg"> // browser tries to fetch horse.mp3 if the above couldn't be recognized <source src="horse.mp3" type="audio/mpeg"> // if none files are valid the browser falls back with a message Your browser does not support the audio element. </audio>

  2. 還請確保您嘗試訪問的文件確實存在。 我嘗試找到它們,但是得到了404響應。

  3. 此外,這行代碼不會執行任何操作:

    lastPlayedFile[0].currentTime = 0;

    請確保你有一個currentTime屬性集在每個的audioArray元素( song1song2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM