The most powerful thing about
HTML5 is the processing of media files. For example, using a simple vedio tag can realize video playback. Similarly, there is a corresponding tag for processing audio files in HTML5, that is, the audio tag. Through this article, I will introduce to you the effect of HTML5 using the Audio tag to achieve lyrics synchronization. Friends who are interested can learn together.
The most powerful thing about HTML5 is the processing of media files. For example, video playback can be achieved by using a simple vedio tag. Similarly, there are corresponding tags for processing audio files in HTML5, that is, the audio tag
HTML5 has been out for so long, but the audio tag in it has only been used once. Of course, it is just to insert this tag. to the page. This time I just took advantage of helping a friend to create a few pages and practice using the audio tag.
First you need to insert an audio tag into the page. Note that it is best not to set loop='loop' here. This attribute is used to set the loop playback, because it will be played later. When you need to use the ended attribute, if loop is set to loop, the ended attribute will always be false.
autoplay='autoplay'Set to automatically play music after the page is loaded. The preload and autoplay attributes have the same effect. If the autoplay attribute appears in the tag, the preload attribute will be ignored.
controls='controls'Set the control bar for displaying music.
XML/HTML Code复制内容到剪贴板 <audio src="music/Yesterday Once More.mp3" id="aud" autoplay="autoplay" controls="controls" preload="auto"> 您的浏览器不支持audio属性,请更换浏览器在进行浏览。 </audio>
After you have this tag, congratulations, your page can already play music. But this would make the page too monotonous, so I added some things to the page so that the lyrics can be displayed on the page synchronously and the music to be played can also be selected. So first to achieve this effect, we have to download some lyrics files in lrc format, and then you need to format the music. Because the initial music file is like this
We need to insert each lyric into a two-digit array and format it After that, the lyrics will become in this format
Attached here is the code for formatting the lyrics
##
XML/HTML Code复制内容到剪贴板 //歌词同步部分 function parseLyric(text) { //将文本分隔成一行一行,存入数组 var lines = text.split('\n'), //用于匹配时间的正则表达式,匹配的结果类似[xx:xx.xx] pattern = /\[\d{2}:\d{2}.\d{2}\]/g, //保存最终结果的数组 result = []; //去掉不含时间的行 while (!pattern.test(lines[0])) { lineslines = lines.slice(1); }; //上面用'\n'生成生成数组时,结果中最后一个为空元素,这里将去掉 lines[lines.length - 1].length === 0 && lines.pop(); lines.forEach(function(v /*数组元素值*/ , i /*元素索引*/ , a /*数组本身*/ ) { //提取出时间[xx:xx.xx] var time = v.match(pattern), //提取歌词 vvalue = v.replace(pattern, ''); //因为一行里面可能有多个时间,所以time有可能是[xx:xx.xx][xx:xx.xx][xx:xx.xx]的形式,需要进一步分隔 time.forEach(function(v1, i1, a1) { //去掉时间里的中括号得到xx:xx.xx var t = v1.slice(1, -1).split(':'); //将结果压入最终数组 result.push([parseInt(t[0], 10) * 60 + parseFloat(t[1]), value]); }); }); //最后将结果数组中的元素按时间大小排序,以便保存之后正常显示歌词 result.sort(function(a, b) { return a[0] - b[0]; }); return result; }
XML/HTML Code复制内容到剪贴板 function fn(sgname){ $.get('music/'+sgname+'.lrc',function(data){ var str=parseLyric(data); for(var i=0,li;i<str.length;i++){ li=$('<li>'+str[i][1]+'</li>'); $('#gc ul').append(li); } $('#aud')[0].ontimeupdate=function(){//视屏 音频当前的播放位置发生改变时触发 for (var i = 0, l = str.length; i < l; i++) { if (this.currentTime /*当前播放的时间*/ > str[i][0]) { //显示到页面 $('#gc ul').css('top',-i*40+200+'px'); //让歌词向上移动 $('#gc ul li').css('color','#fff'); $('#gc ul li:nth-child('+(i+1)+')').css('color','red'); //高亮显示当前播放的哪一句歌词 } } if(this.ended){ //判断当前播放的音乐是否播放完毕 var songslen=$('.songs_list li').length; for(var i= 0,val;i<songslen;i++){ val=$('.songs_list li:nth-child('+(i+1)+')').text(); if(val==sgname){ i=(i==(songslen-1))?1:i+2; sgname=$('.songs_list li:nth-child('+i+')').text(); //音乐播放完毕之后切换下一首音乐 $('#gc ul').empty(); //清空歌词 $('#aud').attr('src','music/'+sgname+'.mp3'); fn(sgname); return; } } } }; }); } fn($('.songs_list li:nth-child(1)').text());
HTML code
XML/HTML Code复制内容到剪贴板 <p class="songs_cnt"> <ul class="songs_list"> <li>Yesterday Once More</li> <li>You Are Beautiful</li> </ul> <button class="sel_song">点<br/><br/>我</button> </p> <p id="gc"> <ul></ul> </p>
css code
XML/HTML Code复制内容到剪贴板 #gc{ width: 400px; height: 400px; background: transparent; margin: 100px auto; color: #fff; font-size: 18px; overflow: hidden; position: relative; } #gc ul{ position: absolute; top: 200px; } #gc ul li{ text-align: center; height: 40px; line-height: 40px; } .songs_cnt{ float: left; margin-top: 200px; position: relative; } .songs_list{ background-color: rgba(0,0,0,.2); border-radius: 5px; float: left; width: 250px; padding: 15px; margin-left: -280px; } .songs_list li{ height: 40px; line-height: 40px; font-size: 16px; color: rgba(255,255,255,.8); cursor: pointer; } .songs_list li:hover{ font-size: 20px; color: rgba(255,23,140,.6); } .sel_song{ position: absolute; top: 50%; width: 40px; height: 80px; margin-top: -40px; font-size: 16px; text-align: center; background-color: transparent; border: 1px solid #2DCB70; font-weight: bold; cursor: pointer; border-radius: 3px; font-family: sans-serif; transition:all 2s; -moz-transition:all 2s; -webkit-transition:all 2s; -o-transition:all 2s; } .sel_song:hover{ color: #fff; background-color: #2DCB70; } .songs_list li.active{ color: #f00; }
jscode
XML/HTML Code复制内容到剪贴板 $('.songs_list li:nth-child(1)').addClass('active'); $('.songs_cnt').mouseenter(function () { var e=event||window.event; var tag= e.target||e.srcElement; if(tag.nodeName=='BUTTON'){ $('.songs_list').animate({'marginLeft':'0px'},'slow'); } }); $('.songs_cnt').mouseleave(function () { $('.songs_list').animate({'marginLeft':'-280px'},'slow'); }); $('.songs_list li').each(function () { $(this).click(function () { $('#aud').attr('src','music/'+$(this).text()+'.mp3'); $('#gc ul').empty(); fn($(this).text()); $('.songs_list li').removeClass('active'); $(this).addClass('active'); }); })
The above is the detailed content of Detailed introduction to the effect of HTML5 using Audio tags to achieve lyrics synchronization. For more information, please follow other related articles on the PHP Chinese website!