0

只有我的前 2 首歌曲正在播放,然后什么也没有发生。我不确定为什么它会工作两次然后停止?我希望它永远播放一系列歌曲。我也不能让我的倒带工作,我想要它,所以当你点击倒带按钮时,歌曲会转到数组上的 -- 如果小于 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;
  }

}
4

2 回答 2

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 的后备

使用 flash 对象的工作脚本示例如下:http : //pastebin.com/WDzEQGtn

于 2013-06-30T08:12:42.707 回答
0

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

您没有向我们展示单击按钮的绑定,但无论如何我会单独绑定每个按钮,因此您无需检查单击了哪个按钮。

于 2013-06-30T08:13:13.630 回答