JS学习笔记(6)--音乐播放器

时间:2023-03-09 14:48:47
JS学习笔记(6)--音乐播放器

说明(2017.3.15):

1. lrc.js里面存储LRC歌词的格式的数组,获取里面的时间轴,转为秒数。

2. 通过audio.currentTime属性,setinterval每秒获取歌曲播放的秒数。

3. 将两个时间比大小,如果“歌曲播放时间”>“歌词时间”,就输出这句歌词。

4. 补充需求:

(1)需要把歌词补充完整

(2)不是在控制台输出,直接在页面输出

(3)增加播放列表

music.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<audio src="Taylor Swift - Love Story.mp3" id="audio" autoplay="autoplay" controls="controls" preload="auto">
您的浏览器不支持audio属性,请更换浏览器再进行浏览。
</audio>
<button id="btnTime">time</button>
</body>
<script src="lrc.js"></script>
<script type="text/javascript">
var text = "[00:15.80]We were both young when I first saw you\
[00:19.74]I closed my eyes and the flashback starts\
[00:23.26]I'm standing there";
var audio = document.getElementById("audio");
btnTime.onclick = function(){
var timeLrc = getTime();
console.log(timeLrc);
}; var getTime = function(){
// 不需要这个函数了,直接输出audio.currentTime这个时间进行比大小就可以
// 获取03:14:33这种格式的当前播放时间
var timeNow = audio.currentTime
// console.log(timeNow);
// 获取分钟数
var timeMin = String(Math.floor(timeNow/60));
// 如果分钟数是1位,前面加个0
timeMin = timeMin.length<2 ? "0"+timeMin : timeMin;
// console.log(timeMin);
var timeSec = String(Math.floor(timeNow%60));
timeSec = timeSec.length<2 ? "0"+timeSec : timeSec;
// console.log(timeSec);
var timeMil = String(timeNow);
timeMil = timeMil.substr(timeMil.indexOf('.')+1,2)
// console.log(timeMil);
var timeLrc = timeMin + ":" + timeSec + "." + timeMil; return timeLrc;
};
var getLrcTime = function(i){
// 获取歌词里的每句的时间
var lrcTime = loveStory[i].substr(1,8);
// 分钟转数字可以去掉前面的0
lrcTimeMin = parseInt(lrcTime.split(":")[0]);
// 虽然末尾有0,不过要转成数字比大小
lrcTimeSec = parseFloat(lrcTime.split(":")[1]);
lrcTime = lrcTimeMin*60+lrcTimeSec;
// console.log(lrcTimeMin);
// console.log(lrcTimeSec);
// console.log(lrcTime);
return lrcTime;
};
// getLrcTime(); setInterval(function(){
// 获取lrc.js文件中的歌词,每秒刷新一下,获取播放时间,然后跟歌词里的时间比对,如果播放时间大于歌词时间,就显示歌词。 var timeNow = audio.currentTime for(var i = 0; i < loveStory.length; i++){
var lrcTime = getLrcTime(i);
// console.log(lrcTime);
var lrcWord = loveStory[i].substr(10,loveStory[i].length);
if(timeNow > lrcTime){
console.log(lrcTime);
console.log(lrcWord);
loveStory.splice(i,1);
}else{ }
}
// if (!audio.paused) {
// console.log(playTime.substr(0,5));
// // console.log(playTime);
// }
},1000);
</script>
</html>

lrc.js

 var loveStory = [
"[00:15.80]We were both young when I first saw you", "[00:19.74]I closed my eyes and the flashback starts", "[00:23.26]I'm standing there", "[00:26.95]On a balcony in summer air", "[00:32.14]See the lights see the party the ball gowns", "[00:35.87]I see you make your way through the crowd", "[00:39.29]And say hello", "[00:43.38]Little did I know", "[00:48.07]That you were Romeo you were throwing pebbles", "[00:51.72]And my daddy said stay away from Juliet", "[00:55.38]And I was crying on the staircase", "[00:58.28]Begging you please don't go", "[01:02.74]And I said", "[01:04.25]Romeo take me somewhere we can be alone", "[01:08.38]I'll be waiting all there's left to do is run"
];