我正在尝试在HTML 5音频标记中实现播放暂停功能
这是我的标签:
<audio id="player">
</audio>
我使用api提供音频src,api填充数组对象&#39;播放列表&#39; ,加载音频源的功能是:
function loadTrack(num)
{
$('#player')[0].src = playlist[num].stream;
trackID = playlist[num].id;
sendTrackInfo(num);
performAction('continue');
}
实际控制的代码就是这个(我已经展示了整个功能,但我想我们只需处理&#39;暂停&#39;播放&#39;节点) :
function performAction(input)
{
switch(input)
{
case 'pause':
currentTime= getCurrentTime();
$('#player')[0].pause();
uiObj['controls'].performAction('pause');
playing=false;
break;
case 'play':
setCurrentTime(currentTime);
console.log(getCurrentTime()); //Here the output is proper i.e current time
loadTrack(trackNum);
$('#player')[0].play();
uiObj['controls'].performAction('play');
playing=true;
case 'previous':
if($('#player')[0].currentTime > 3)
{
$('#player')[0].currentTime = 0;
return;
}
else if(repeat)
{
$('#player')[0].currentTime = 0;
performAction('continue');
}
else
{
trackNum --;
if(trackNum < 0)
{
performAction('reset');
}
else
{
loadTrack(trackNum);
}
}
break;
case 'next':
if(repeat)
{
$('#player')[0].currentTime = 0;
performAction('continue');
}
else
{
trackNum ++;
if(trackNum >= trackTotal)
{
trackNum = 0;
loadTrack(trackNum);
if(!repeatAll)
{
performAction('pause');
}
}
else
{
loadTrack(trackNum);
}
}
break;
case 'repeat':
if(!repeat && !repeatAll)
{
repeat = true;
uiObj['controls'].performAction('repeat');
}
else if(repeat)
{
repeat = false;
repeatAll = true;
uiObj['controls'].performAction('repeatAll');
}
else
{
repeatAll = false;
uiObj['controls'].performAction('repeatNone');
}
break;
case 'shuffle':
var playlistcopy;
var r;
shuffle = !shuffle;
if(shuffle)
{
playlistcopy = playlist.slice(0);
playlist = [];
for (var i = 0; i < trackTotal; i++)
{
r = Math.floor(Math.random() * (playlistcopy.length));
playlist.push(playlistcopy[r]);
playlistcopy.splice(r,1);
}
setTrackNum(true);
uiObj['trackInfo'].updateUI();
uiObj['controls'].performAction('shuffle');
}
else
{
playlist = scObj.getBaseTracks();
setTrackNum(false);
uiObj['trackInfo'].updateUI();
uiObj['controls'].performAction('unshuffle');
}
break;
case 'continue':
if(playing) $('#player')[0].play();
else $('#player')[0].pause();
break;
case 'ended':
if(repeat)
{
performAction('restart');
}
else
{
performAction('next');
}
break;
case 'restart':
$('#player')[0].currentTime = 0;
$('#player')[0].play();
break;
case 'reset':
playlist = scObj.getBaseTracks();
setTrackNum(false);
uiObj['trackInfo'].updateUI();
uiObj['controls'].performAction('unshuffle');
repeat = false;
repeatAll = false;
uiObj['controls'].performAction('repeatNone');
trackNum = 0;
loadTrack(trackNum);
break;
case 'reload':
playlist = scObj.getBaseTracks();
trackNum = 0;
trackTotal = playlist.length;
loadTrack(trackNum);
performAction('continue');
break;
default:
break;
}
}
歌曲播放的麻烦很好,功能正常,但当我暂停并尝试再播放一首歌时,它从开始而不是暂停时间开始。
提前致谢。