前言
最近自己寫了個移動端音樂播放器,用了很多框架,也踩了很多坑,感覺比較復(fù)雜的就是計(jì)算歌詞實(shí)時播放了,所以想著大概給整理了一下,分享出來,有問題的話朋友們可以指出,有需要的朋友們也可以學(xué)習(xí)學(xué)習(xí)。
布局
(歌詞都是后臺數(shù)據(jù)獲取,方便理解我把所有歌詞都一一列出來)
<section> <figure> <img id="animat" src="http://www.gimoo.net/t/1808/img/img3.jpg"/> </figure> <div class="lyric"> <div class="lrc-list"> <p data-play="audio-t-0" data-index="0">逃跑計(jì)劃 </p> <p data-play="audio-t-1" data-index="1">一萬次悲傷 </p> <p data-play="audio-t-2" data-index="2">oh honey </p> <p data-play="audio-t-3" data-index="3">我腦海里全都是你 </p> <p data-play="audio-t-8" data-index="4">oh無法抗拒的心情 </p> <p data-play="audio-t-13" data-index="5">難以呼吸</p> <p data-play="audio-t-15" data-index="6">tonight </p> <p data-play="audio-t-17" data-index="7">是否又要錯過一個夜晚</p> <p data-play="audio-t-23" data-index="8">是否還要掩飾最后的期待 </p> <p data-play="audio-t-29" data-index="9">oh tonight </p> <p data-play="audio-t-30" data-index="10">一萬次悲傷 </p> <p data-play="audio-t-33" data-index="11">依然會有意義</p> <p data-play="audio-t-35" data-index="12">我一直在最溫暖的地方等你</p> <p data-play="audio-t-40" data-index="13">似乎只能這樣停留一個方向</p> <p data-play="audio-t-46" data-index="14">已不能改變</p> <p data-play="audio-t-51" data-index="15">每一顆眼淚是遺忘的光 </p> <p data-play="audio-t-57" data-index="16">最昏暗的地方也變得明亮</p> <p data-play="audio-t-61" data-index="17">我奔涌的暖流尋找你的海洋 </p> <p data-play="audio-t-67" data-index="18">我注定這樣</p> <p data-play="audio-t-73" data-index="19">oh honey </p> <p data-play="audio-t-75" data-index="20">你目光里充滿憂郁 </p> <p data-play="audio-t-81" data-index="21">就像經(jīng)歷一遍飛行難以逃避</p> <p data-play="audio-t-87" data-index="22">tonight </p> <p data-play="audio-t-89" data-index="23">是否還要錯過這個夜晚</p> <p data-play="audio-t-91" data-index="24">是否還要熄滅所有的期待</p> <p data-play="audio-t-95" data-index="25">tonight</p> <p data-play="audio-t-100" data-index="26">一萬次悲傷</p> <p data-play="audio-t-102" data-index="27">依然會有意義</p> <p data-play="audio-t-106" data-index="28">我一直在最后的地方等你</p> <p data-play="audio-t-109" data-index="29">似乎只能這樣</p> <p data-play="audio-t-114" data-index="30">停留一個方向</p> <p data-play="audio-t-120" data-index="31">已不能改變</p> <p data-play="audio-t-123" data-index="32">每一顆眼淚是遺忘的光</p> <p data-play="audio-t-130" data-index="33">最昏暗的地方也變得明亮</p> <p data-play="audio-t-135" data-index="34">我奔涌的暖流尋找你的海洋</p> <p data-play="audio-t-141" data-index="35">我注定這樣</p> <p data-play="audio-t-155" data-index="36">一萬次悲傷 </p> <p data-play="audio-t-189" data-index="37">依然會有意義</p> <p data-play="audio-t-192" data-index="38">我一直在最后的地方等你</p> <p data-play="audio-t-197" data-index="39">似乎只能這樣</p> <p data-play="audio-t-200" data-index="40">停留一個方向 </p> <p data-play="audio-t-204" data-index="41">已不能改變</p> <p data-play="audio-t-208" data-index="42">每一顆眼淚是遺忘的光</p> <p data-play="audio-t-213" data-index="43">最昏暗的地方也變得明亮</p> <p data-play="audio-t-218" data-index="44">我奔涌的暖流尋找你的海洋</p> <p data-play="audio-t-225" data-index="45">我注定這樣</p> <p data-play="audio-t-236" data-index="46">oh honey </p> <p data-play="audio-t-238" data-index="47">我腦海里全都是你 </p> <p data-play="audio-t-243" data-index="48">oh無法抗拒的心情 </p> <p data-play="audio-t-247" data-index="49">難以呼吸</p> </div> </div> </section>
注意 : 給每一句歌詞都自定義兩個屬性,data-play屬性為了計(jì)算什么時候顯示該句歌詞,它的屬性值audio-t- “num” 是表示這句歌詞在num秒的時候高亮顯示; data-index為了計(jì)算什么時候歌詞往上走,就按順序定義就行。
以下是靜態(tài)效果圖:
(效果圖沒有歌詞是因?yàn)樽蠡懦霈F(xiàn)歌詞)
左滑出現(xiàn)歌詞
$('figure').on("swipeLeft" , function(){ $('figure').css('display' , 'none'); $('.lyric').css('display' , 'block').addClass("animated slideInRight"); }) $('.lyric').on("swipeRight" , function(){ $('.lyric').css('display' , 'none'); $('figure').css('display' , 'block').addClass("animated slideInLeft"); })
歌詞出現(xiàn):
重點(diǎn)來了
function lyricPlay(){ var timer2; var $cur = null; var $lrcbox = $(".lrc-list"); $lrcwrap = $lrcbox.parent(); clearInterval(timer2); timer2 = setInterval( function(){ var time = Math.ceil( $('audio')[0].currentTime); //currentTime 是獲取音樂實(shí)時播放時間 $cur = $("audio-t-"+time).selector;//拿到自定義屬性data-play $('p' , $lrcbox ).each(function(){ //循環(huán)拿到p var playT = $(this).data('play');//當(dāng)前播放元素屬性值 if( playT == $cur ){ //給符合條件歌詞加上高亮并刪除兄弟元素的class名 $(this).stop().addClass('acitve').siblings().removeClass('acitve'); }else{ return; } var index = $(this).data("index"); //當(dāng)前元素下標(biāo) var lineHeight =$(this).height() ; //一行歌詞高度 var boxHeight = $lrcwrap.height(); //歌詞顯示區(qū)域高度 var screensize = boxHeight / lineHeight; //一屏顯示多少句歌詞 var half = Math.floor(screensize / 2); //半屏歌詞數(shù)量 //當(dāng)前歌詞超過半屏 if(index > half){ //計(jì)算出超過的高度 減去 一行歌詞的高度 var top = (half - index) * lineHeight + lineHeight $lrcbox.css({ "top" : parseInt(top) + "px" }); } }) } , 1000) } lyricPlay();
效果如下:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,當(dāng)然以上方法都只是我自己的想法,如果不足之處歡迎大家指點(diǎn),希望本文的內(nèi)容對大家能有一定的幫助。