HTML5音樂播放器


初學HTML5,感覺audio標簽有點興趣。嘗試着做一個播放器,練習過程中發現,audio的ended事件觸發有點小問題第一首播放結束確實只觸發了一次ended事件,第二次播放接受觸發兩次,第三次播放結束觸發了4次。導致一直在列表的1、2兩首輪播,完全不顧及第三首的感受。。 在幾大論壇上發帖求助未果,於是自己一陣亂搞,加了個判斷 if(self.media.currentTime == self.media.duration)如果不滿足這個條件觸發了ended事件也不去播放下一首。

function Audio(song, playType, dom){
/*
* 播放器構造函數。
* dom:為audio元素,可以不傳。
* song : 為歌曲列表,只支持數組形式,格式為[{}{}]
* playType 為播放方式: 1 順序播放 2 隨機播放 3 單曲循環 4 全部循環
*/
if(!dom) {
this.media = document.createElement('audio');
document.body.appendChild(this.media);
}else {
this.media = typeof dom == 'string' ? document.getElementById(dom) : dom;
}
this.currentIndex = 0;
this.songList = song;
this.countTotal = this.songList.length;
this.playType = playType || 1;
this.MusicInfo = [];
this.playing = false;
}
/*
* 播放器啟動主函數
*/
Audio.prototype.startPlay = function(){
this.media.src = this.songList[this.currentIndex].src;
this._play();
}

/*
* 播放器播放核心函數.
*/
Audio.prototype._play = function(){
var self = this;
this.media.play();
this.playing = true;
this.mediaEvent('ended' ,callback);
function callback(){
//單曲循環無需單獨處理,只需直接調用startPlay()函數。
if(self.media.currentTime == self.media.duration){
switch(self.playType){
case 1:
if(self.currentIndex == self.countTotal-1){
return false;
}else{
self.currentIndex++;
}
break;
case 2:
self.currentIndex = Math.floor(Math.random()*self.countTotal);
break;
case 4:
self.currentIndex++;
console.log("self.currentIndex==",self.currentIndex);
self.currentIndex = (self.currentIndex > self.countTotal-1) ? 0 : self.currentIndex;
break;
}
self.startPlay();
}
}
}
/*
*播放下一首如果當前已經是最后一首則播放第一首
*/
Audio.prototype.playNext = function(){
this.currentIndex++;
this.currentIndex = this.currentIndex > this.countTotal-1 ? 0 : this.currentIndex;
this.startPlay();
}
/*
*播放上一首如果當前已經是第一首則播放最后一首
*/
Audio.prototype.playPrevious = function(){
this.currentIndex++;
this.currentIndex = this.currentIndex < 0 ? this.countTotal-1 : this.currentIndex;
this.startPlay();

}

/*
* 暫停當前播放,如果傳回調函數,則暫停后執行回調。
*/
Audio.prototype.playPause = function(callback){
if(this.playing){
this.media.pause();
this.playing = false;
}else{
this.media.play();
this.playing = true;
}
if(!callbakc){callback();}
}

/*
* 獲取當前播放位置
*/
Audio.prototype.getCurrentTime = function(){
return this.media.currentTime;
}

/*
* 播放器各種事件監聽.
* tip 類型必須是正確的類型
*/
Audio.prototype.mediaEvent = function(eventType, callback){

Event.add(this.media,eventType,callback);
}

/*
* 播放用戶自定義時間,即拖動進度條。
*/
Audio.prototype.playUserTime = function(time){

this.media.currentTime = time;
}
/*
* 獲取當前媒體信息
* src 當前媒體路徑
* size 當前媒體總時長.
*/
Audio.prototype.getMusicInfo = function(){
this.MusicInfo.src = this.media.currentSrc;
this.MusicInfo.size = this.media.duration;
this.MusicInfo.name = this.songList[this.currentIndex].name;
return this.MusicInfo;
}
/*
* 設置或者獲取當前音量
* voluems的值需大於0 小於等於 1
*/
Audio.prototype.setVolume = function(volumes){
if(volumes) {
this.media.volume = volumes;
}else{
return this.media.volume;
}
}
/*
* 設置或者取消靜音.
* flag的值為true是靜音,false時正常
*/
Audio.prototype.muted = function(flag){
if(flag){
this.media.muted = 1;
}else{
this.media.muted = 0;
}
}
/*
* 向播放列表添加新歌曲
* song為所需要添加的歌曲,可以多首,格式如構造函數中song.
*/
Audio.prototype.addSongToList = function(song){
this.songList.push(song);
this.countTotal = this.songList.length;
}

Audio.prototype.getBuffered = function(){
return this.media.buffered;
}
/*全局事件監聽封裝函數*/
var Event = {
add : function(node, eventType, callback){
var node = typeof node == 'string' ? document.getElementById(node) : node;
if(document.addEventListener){
node.addEventListener(eventType, callback, false);
}else{
node.attachEvent('on' + eventType, callback);
}
},
remove : function(node, eventType, callback){
var node = typeof node == 'string' ? document.getElementById(node) : node;
if(document.removeEventListener){
node.removeEventListener(eventType, callback, false);
}else{
node.detachEvent('on' + eventType, callback);
}
}
}

var core = {
formatPlayTime : function(tempTime){
var temp = tempTime.toString().split(".")[0];
if(tempTime<=60){
temp = temp>=10? temp : "0"+temp;
return "00 : " + temp;
}else{
var minute =Math.floor(temp/60);
minute = (minute >= 10)? minute : "0"+ minute;
var second = temp%60;
second = (second >= 10)? second : "0"+second;
return minute + " : " + second;
}
}
}


  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM