点击点击按钮,我需要播放下一首歌曲

时间:2018-06-16 14:08:30

标签: javascript html5-audio

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网络浏览器。这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

  1. 请确保您使用<audio>标记,如here所示。浏览器将获取第一个可识别的来源:

    <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元素(audioArraysong1

  4. 上设置了song2属性
相关问题