html5音频歌曲只播放阵列中的前2个?

时间:2013-06-30 06:11:49

标签: javascript html5 audio

只有我的前两首歌在播放,然后没有任何反应。 我不确定为什么它会工作两次然后停止?我想让它永远通过一系列歌曲播放。 我也无法让我的倒带工作,我想要它,所以当你点击倒带按钮时,歌曲会转到 - 在数组上 - 如果小于0则转到array.length歌曲。

全局:

var btnPlay = new Button(561,743,33,92);
var btnRewind = new Button(0,0,800,300); //entire canvas just to test

var song1 = new Audio("songs/loop1.mp3");
var song2 = new Audio("songs/loop2.mp3");
var song3 = new Audio("songs/loop3.mp3");
var song4 = new Audio("songs/loop4.mp3");
var song5 = new Audio("songs/loop5.mp3");
var song6 = new Audio("songs/loop6.mp3");
var song7 = new Audio("songs/loop7.mp3");
var song8 = new Audio("songs/loop8.mp3");
var song9 = new Audio("songs/loop9.mp3");
var song10 = new Audio("songs/loop10.mp3");
var song11 = new Audio("songs/loop11.mp3");
var rewindClicked = false;
var songList = [];
var currentSong = 1;

songList.push(song1,song2,song3,song4,song5,song6,song7,song8,song9,song10,song11);

var song = songList[0];

快退按钮点击方法:

function mouseClickedButtons(e)
{
alert("hi"); //works
   if(btnRewind.checkClicked())
   {
   alert("hey"); //does not
   rewindClicked = true;
     NextSong();

   }

}

改变歌曲方法:

function NextSong()
{
document.removeEventListener('ended',NextSong);

if(rewindClicked ==true)
{
currentSong--;
}
else
{
currentSong++;
}

if(currentSong > songList.length);
{
currentSong = 1;
}

if(currentSong < 0)
{
currentSong = songList.length; 
}

songList[currentSong].play();
rewindClicked = false;




songList[currentSong].addEventListener('ended', NextSong);
}

监听器:

song.addEventListener('ended', NextSong);

按钮对象(我在播放按钮中工作,所以它可以工作)

function Button(xL,xR,yT,yB)
{
this.xLeft = xL;
this.xRight = xR;
this.yTop = yT;
this.yBottom = yB;
}



Button.prototype.checkClicked = function()
{

  if(this.xLeft <= mouseX && mouseX <= this.xRight && this.yTop <= mouseY && mouseY <= this.yBottom)
  {

     return true;
  }

}

2 个答案:

答案 0 :(得分:0)

播放HTML5音频的基本代码也包含控件:

var i = 0; // song index
var songList = [
    'songs/loop1.mp3', 'songs/loop2.mp3', 'songs/loop3.mp3', 'songs/loop4.mp3',
    'songs/loop5.mp3', 'songs/loop6.mp3', 'songs/loop7.mp3', 'songs/loop8.mp3'
];

// create audio element
var ae = document.createElement('audio');
ae.id = 'audioplayer';
ae.src = songList[0];
document.body.appendChild(ae);
// create controls
var btnPlay = document.createElement('button');
var btnPrev = document.createElement('button');
var btnNext = document.createElement('button');
var testDiv = document.createElement('div');
btnPlay.innerHTML = 'Play/Pause';
btnPrev.innerHTML = 'Previous song';
btnNext.innerHTML = 'Next song';
testDiv.id = "test";
document.body.appendChild(btnPlay);
document.body.appendChild(btnPrev);
document.body.appendChild(btnNext);
document.body.appendChild(testDiv);

//add event handlers
ae.onplay = function () {
    testDiv.innerHTML = 'Now playing #' + (i+1) + ' ' + songList[i];
};
ae.onended = function(){
    NextSong();
};
btnPlay.onclick = function() {
    if (ae.paused) ae.play();
    else ae.pause();
};
btnPrev.onclick = PrevSong;
btnNext.onclick = NextSong;
ae.play();

function PrevSong() {
    i = (i > 0)? i - 1 : songList.length - 1; // choose previous index
    ae.setAttribute("src", songList[i]);
    ae.play();
}
function NextSong() {
    i = (i < songList.length - 1)? i + 1 : 0; // choose next index
    ae.setAttribute("src", songList[i]);
    ae.play();
}

工作小提琴:http://jsfiddle.net/F35Fw/1/

IE8和旧版本的Firefox不支持HTML5音频标签和mp3播放,因此为了向后兼容,您应该使用一些flash-based fallback

工作脚本with the flash object的示例位于:http://pastebin.com/WDzEQGtn

答案 1 :(得分:0)

checkClicked在哪里定义?似乎对该功能的调用无法正常运行。

你没有向我们展示点击按钮的绑定,但无论如何我会单独绑定每个按钮,所以你不需要检查点击了哪个按钮。