diff --git a/js/main.js b/js/main.js index 5314c8c..d03c331 100644 --- a/js/main.js +++ b/js/main.js @@ -1,7 +1,6 @@ window.onload = function(){ createlist(); InitPlayMode(); - checkplayInterval = setInterval(checkplay, 3000); } // Title Controller @@ -100,6 +99,93 @@ class TitleStat{ } } +class PlayerControl{ + constructor() { + + } + + playerObject = document.getElementById('Player'); + ListenerStatus = false; + + // 创建事件监听器 + enableListener( func ) { + this.playerObject.addEventListener('ended', func()); + this.ListenerStatus = true; + console.log("启用播放器播放结束事件监听"); + } + + // 移除事件监听器 + disableListener( func ) { + this.playerObject.removeEventListener('ended', func()); + this.ListenerStatus = false; + console.log("停用播放器播放结束事件监听"); + } + + // 获取监听器状态 + getListenerStatus() { + return this.ListenerStatus; + } + + /* 默认函数 */ + // 播放状态检测 + playStatCheck(){ + // 是否单曲? + if ( CyclePlayMode == 2 ){ + // 从头开始 + this.playerObject.play(); + console.log("洗脑的单曲循环!"); + } + + // 播放到列表尾部,检查播放状态是否为0(单次播放列表) + if ( nowPlay >= (list.size() + 1) ) { + if (CyclePlayMode == 0) { + if( this.ListenerStatus ) { + // 播放列表完毕就清除事件监听器 + this.disableListener(this.playStatCheck()); + console.log("播放完毕,已清除事件监听。"); + } else { + // 事件监听器未打开 + console.error("关闭失败,事件监听器未启用。"); + } + } + } + // 切歌 + playnext(); + + // 确认当前播放模式 + // switch ( CyclePlayMode ){ + // case 0: // 列表播放(仅一次) + // if ( nowPlay >= (list.size() + 1) ) { + // if( this.ListenerStatus ) { + // // 播放列表完毕就清除事件监听器 + // this.disableListener(this.playStatCheck()); + // console.log("播放完毕,已清除事件监听。"); + // } else { + // // 事件监听器未打开 + // console.error("关闭失败,事件监听器未启用。"); + // } + // } + // break; + // case 1: // 列表循环 + // if ( !this.ListenerStatus ) { + // // 如果未启用监听器,则启用 + // this.enableListener(this.playStatCheck()); + // console.log("启用列表,已清除事件监听。") + // } + + // break; + // case 2: // 单曲循环 + // // 持续不断的从头开始 + // this.playerObject.play(); + // break; + // default: + // // 缺省值 + // console.error("模式错误,疑似bug或者JS已被修改,请注意!") + // break; + // } + } +} + // Title Control // var originTitle = document.title; let TitleControl = new TitleStat(document.title); @@ -347,8 +433,7 @@ function processCyclePlay(){ switch ( CyclePlayMode ){ case 0: // OFF if ( nowPlay >= (list.size() - 1) ) { - clearInterval(checkplayInterval); - console.log("顺序播放已完成,已清除播放状态监听。"); + } break; case 1: // single cycle @@ -363,21 +448,6 @@ function processCyclePlay(){ } } -// 检测是否播放完毕 -function checkplay(){ - // 为了防止播放列表结束以后一直调用playnext产生过多的alart,所以需要检测播放列表是否为空 - if(!list.isEmpty()){ - if(document.getElementById("Player").ended){ - // 播放完毕后自动播放下一首 - playnext(); - } - }else{ - //clearInterval(checkplay); - console.log("播放列表为空,已清除播放状态监听。"); - clearInterval(checkplayInterval) - } -} - // 播放列表 function playthelist(){ if(list.isEmpty()){