HTML5使用Audio标签实现歌词同步的效果
生活随笔
收集整理的這篇文章主要介紹了
HTML5使用Audio标签实现歌词同步的效果
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HTML5的最強(qiáng)大之處莫過于對(duì)媒體文件的處理,如利用一個(gè)簡(jiǎn)單的vedio標(biāo)簽就可以實(shí)現(xiàn)視頻播放。類似地,在HTML5中也有對(duì)應(yīng)的處理音頻文件的標(biāo)簽,那就是audio標(biāo)簽?
HTML5出來這么久了,但是關(guān)于它里面的audio標(biāo)簽也就用過那么一次,當(dāng)然還僅僅只是把這個(gè)標(biāo)簽插入到了頁(yè)面中。這次呢就剛好趁著幫朋友做幾個(gè)頁(yè)面,拿這個(gè)audio標(biāo)簽來練練手。?
首先你需要向頁(yè)面中插入一個(gè)audio標(biāo)簽,注意這里最好不要設(shè)置loop='loop',這個(gè)屬性使用來設(shè)置循環(huán)播放的,因?yàn)榈胶竺嫘枰褂胑nded屬性的時(shí)候,如果loop被設(shè)置為loop的話,ended屬性將一直是false。?
autoplay='autoplay'設(shè)置頁(yè)面加載后自動(dòng)播放音樂,preload和autoplay屬性的作用是一樣的,如果標(biāo)簽中出現(xiàn)了autoplay屬性,那么preload屬性將被忽略。?
controls='controls'設(shè)置顯示音樂的控制條。?
XML/HTML Code復(fù)制內(nèi)容到剪貼板<audio?src="music/Yesterday?Once?More.mp3"?id="aud"?autoplay="autoplay"?controls="controls"?preload="auto">? ?? 您的瀏覽器不支持audio屬性,請(qǐng)更換瀏覽器在進(jìn)行瀏覽。? ?? </audio>???
//歌詞同步部分? ?? function?parseLyric(text)?{? ?? //將文本分隔成一行一行,存入數(shù)組? ?? var?lines?=?text.split('\n'),? ?? //用于匹配時(shí)間的正則表達(dá)式,匹配的結(jié)果類似[xx:xx.xx]? ?? pattern?=?/\[\d{2}:\d{2}.\d{2}\]/g,? ?? //保存最終結(jié)果的數(shù)組? ?? result?=?[];? ?? //去掉不含時(shí)間的行? ?? while?(!pattern.test(lines[0]))?{? ?? lineslines?=?lines.slice(1);? ?? };? ?? //上面用'\n'生成生成數(shù)組時(shí),結(jié)果中最后一個(gè)為空元素,這里將去掉? ?? lines[lines.length?-?1].length?===?0?&&?lines.pop();? ?? lines.forEach(function(v?/*數(shù)組元素值*/?,?i?/*元素索引*/?,?a?/*數(shù)組本身*/?)?{? ?? //提取出時(shí)間[xx:xx.xx]? ?? var?time?=?v.match(pattern),? ?? //提取歌詞? ?? vvalue?=?v.replace(pattern,?'');? ?? //因?yàn)橐恍欣锩婵赡苡卸鄠€(gè)時(shí)間,所以time有可能是[xx:xx.xx][xx:xx.xx][xx:xx.xx]的形式,需要進(jìn)一步分隔? ?? time.forEach(function(v1,?i1,?a1)?{? ?? //去掉時(shí)間里的中括號(hào)得到xx:xx.xx? ?? var?t?=?v1.slice(1,?-1).split(':');? ?? //將結(jié)果壓入最終數(shù)組? ?? result.push([parseInt(t[0],?10)?*?60?+?parseFloat(t[1]),?value]);? ?? });? ?? });? ?? //最后將結(jié)果數(shù)組中的元素按時(shí)間大小排序,以便保存之后正常顯示歌詞? ?? result.sort(function(a,?b)?{? ?? return?a[0]?-?b[0];? ?? });? ?? return?result;? ?? }??
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(){//視屏?音頻當(dāng)前的播放位置發(fā)生改變時(shí)觸發(fā)? ?? for?(var?i?=?0,?l?=?str.length;?i?<?l;?i++)?{? ?? if?(this.currentTime?/*當(dāng)前播放的時(shí)間*/?>?str[i][0])?{? ?? //顯示到頁(yè)面? ?? $('#gc?ul').css('top',-i*40+200+'px');?//讓歌詞向上移動(dòng)? ?? $('#gc?ul?li').css('color','#fff');? ?? $('#gc?ul?li:nth-child('+(i+1)+')').css('color','red');?//高亮顯示當(dāng)前播放的哪一句歌詞? ?? }? ?? }? ?? if(this.ended){?//判斷當(dāng)前播放的音樂是否播放完畢? ?? 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());???
<div?class="songs_cnt">? ?? <ul?class="songs_list">? ?? <li>Yesterday?Once?More</li>? ?? <li>You?Are?Beautiful</li>? ?? </ul>? ?? <button?class="sel_song">點(diǎn)<br/><br/>我</button>? ?? </div>? ?? <div?id="gc">? ?? <ul></ul>? ?? </div>???
#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;? ?? }???
$('.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');? ?? });? ?? }) ?
HTML5出來這么久了,但是關(guān)于它里面的audio標(biāo)簽也就用過那么一次,當(dāng)然還僅僅只是把這個(gè)標(biāo)簽插入到了頁(yè)面中。這次呢就剛好趁著幫朋友做幾個(gè)頁(yè)面,拿這個(gè)audio標(biāo)簽來練練手。?
首先你需要向頁(yè)面中插入一個(gè)audio標(biāo)簽,注意這里最好不要設(shè)置loop='loop',這個(gè)屬性使用來設(shè)置循環(huán)播放的,因?yàn)榈胶竺嫘枰褂胑nded屬性的時(shí)候,如果loop被設(shè)置為loop的話,ended屬性將一直是false。?
autoplay='autoplay'設(shè)置頁(yè)面加載后自動(dòng)播放音樂,preload和autoplay屬性的作用是一樣的,如果標(biāo)簽中出現(xiàn)了autoplay屬性,那么preload屬性將被忽略。?
controls='controls'設(shè)置顯示音樂的控制條。?
XML/HTML Code復(fù)制內(nèi)容到剪貼板
有了這個(gè)標(biāo)簽之后,那么恭喜你,你的頁(yè)面已經(jīng)可以播放音樂了。但是這樣會(huì)不會(huì)顯得頁(yè)面太過于單調(diào)了,于是我又給頁(yè)面添加了一些東西,讓歌詞能夠同步的顯示在頁(yè)面上,還能夠選擇要播放的音樂。那么先要做成這樣的效果,我們就得要去下載一些lrc格式的歌詞文件,然后你需要把這些音樂格式化一番。因?yàn)閯傞_始的音樂文件是這樣的
我們需要把每一句歌詞插入到一個(gè)二位數(shù)組里面,經(jīng)過格式化之后歌詞就變成這樣的格式了?
這里附上格式化歌詞的代碼
到了這里我們就能夠很容易的使用每首音樂的歌詞了,我們需要有一個(gè)function來獲得歌詞,并且讓他同步的顯示在頁(yè)面上,能夠正常的切換音樂。下面附上代碼。
XML/HTML Code復(fù)制內(nèi)容到剪貼板那么到了這里你的音樂歌詞已經(jīng)能夠正常的同步顯示在頁(yè)面上了。不過還缺少一個(gè)東西,就是一個(gè)音樂的列表,我希望能夠點(diǎn)擊這個(gè)列表里的音樂,從而播放該音樂,下面附上代碼。?
HTML代碼?
css代碼
XML/HTML Code復(fù)制內(nèi)容到剪貼板js代碼
XML/HTML Code復(fù)制內(nèi)容到剪貼板總結(jié)
以上是生活随笔為你收集整理的HTML5使用Audio标签实现歌词同步的效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx + PHP Web服务器
- 下一篇: WebContainers简介:在浏览器