佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > HTML5使用Audio標(biāo)簽實現(xiàn)歌詞同步的效果

HTML5使用Audio標(biāo)簽實現(xiàn)歌詞同步的效果

熱門標(biāo)簽:團購網(wǎng)站 網(wǎng)站排名優(yōu)化 企業(yè)做大做強 科大訊飛語音識別系統(tǒng) 百度競價排名 國美全國運營中心 網(wǎng)站文章發(fā)布 太平洋壽險電話營銷
HTML5的最強大之處莫過于對媒體文件的處理,如利用一個簡單的vedio標(biāo)簽就可以實現(xiàn)視頻播放。類似地,在HTML5中也有對應(yīng)的處理音頻文件的標(biāo)簽,那就是audio標(biāo)簽
HTML5出來這么久了,但是關(guān)于它里面的audio標(biāo)簽也就用過那么一次,當(dāng)然還僅僅只是把這個標(biāo)簽插入到了頁面中。這次呢就剛好趁著幫朋友做幾個頁面,拿這個audio標(biāo)簽來練練手。
首先你需要向頁面中插入一個audio標(biāo)簽,注意這里最好不要設(shè)置loop='loop',這個屬性使用來設(shè)置循環(huán)播放的,因為到后面需要使用ended屬性的時候,如果loop被設(shè)置為loop的話,ended屬性將一直是false。
autoplay='autoplay'設(shè)置頁面加載后自動播放音樂,preload和autoplay屬性的作用是一樣的,如果標(biāo)簽中出現(xiàn)了autoplay屬性,那么preload屬性將被忽略。
controls='controls'設(shè)置顯示音樂的控制條。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <audio src="music/Yesterday Once More.mp3" id="aud" autoplay="autoplay" controls="controls" preload="auto">    
  2. 您的瀏覽器不支持audio屬性,請更換瀏覽器在進行瀏覽。    
  3. </audio>   

有了這個標(biāo)簽之后,那么恭喜你,你的頁面已經(jīng)可以播放音樂了。但是這樣會不會顯得頁面太過于單調(diào)了,于是我又給頁面添加了一些東西,讓歌詞能夠同步的顯示在頁面上,還能夠選擇要播放的音樂。那么先要做成這樣的效果,我們就得要去下載一些lrc格式的歌詞文件,然后你需要把這些音樂格式化一番。因為剛開始的音樂文件是這樣的


我們需要把每一句歌詞插入到一個二位數(shù)組里面,經(jīng)過格式化之后歌詞就變成這樣的格式了
這里附上格式化歌詞的代碼

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. //歌詞同步部分    
  2. function parseLyric(text) {    
  3. //將文本分隔成一行一行,存入數(shù)組    
  4. var lines = text.split('\n'),    
  5. //用于匹配時間的正則表達式,匹配的結(jié)果類似[xx:xx.xx]    
  6. pattern = /\[\d{2}:\d{2}.\d{2}\]/g,    
  7. //保存最終結(jié)果的數(shù)組    
  8. result = [];    
  9. //去掉不含時間的行    
  10. while (!pattern.test(lines[0])) {    
  11. lineslines = lines.slice(1);    
  12. };    
  13. //上面用'\n'生成生成數(shù)組時,結(jié)果中最后一個為空元素,這里將去掉    
  14. lines[lines.length - 1].length === 0 && lines.pop();    
  15. lines.forEach(function(v /*數(shù)組元素值*/ , i /*元素索引*/ , a /*數(shù)組本身*/ ) {    
  16. //提取出時間[xx:xx.xx]    
  17. var time = v.match(pattern),    
  18. //提取歌詞    
  19. vvalue = v.replace(pattern, '');    
  20. //因為一行里面可能有多個時間,所以time有可能是[xx:xx.xx][xx:xx.xx][xx:xx.xx]的形式,需要進一步分隔    
  21. time.forEach(function(v1, i1, a1) {    
  22. //去掉時間里的中括號得到xx:xx.xx    
  23. var t = v1.slice(1, -1).split(':');    
  24. //將結(jié)果壓入最終數(shù)組    
  25. result.push([parseInt(t[0], 10) * 60 + parseFloat(t[1]), value]);    
  26. });    
  27. });    
  28. //最后將結(jié)果數(shù)組中的元素按時間大小排序,以便保存之后正常顯示歌詞    
  29. result.sort(function(a, b) {    
  30. return a[0] - b[0];    
  31. });    
  32. return result;    
  33. }  

到了這里我們就能夠很容易的使用每首音樂的歌詞了,我們需要有一個function來獲得歌詞,并且讓他同步的顯示在頁面上,能夠正常的切換音樂。下面附上代碼。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. function fn(sgname){    
  2. $.get('music/'+sgname+'.lrc',function(data){    
  3. var str=parseLyric(data);    
  4. for(var i=0,li;i<str.length;i++){    
  5. li=$('<li>'+str[i][1]+'</li>');    
  6. $('#gc ul').append(li);    
  7. }    
  8. $('#aud')[0].ontimeupdate=function(){//視屏 音頻當(dāng)前的播放位置發(fā)生改變時觸發(fā)    
  9. for (var i = 0l = str.length; i < l; i++) {    
  10. if (this.currentTime /*當(dāng)前播放的時間*/ > str[i][0]) {    
  11. //顯示到頁面    
  12. $('#gc ul').css('top',-i*40+200+'px'); //讓歌詞向上移動    
  13. $('#gc ul li').css('color','#fff');    
  14. $('#gc ul li:nth-child('+(i+1)+')').css('color','red'); //高亮顯示當(dāng)前播放的哪一句歌詞    
  15. }    
  16. }    
  17. if(this.ended){ //判斷當(dāng)前播放的音樂是否播放完畢    
  18. var songslen=$('.songs_list li').length;    
  19. for(var i0,val;i<songslen;i++){    
  20. val=$('.songs_list li:nth-child('+(i+1)+')').text();    
  21. if(val==sgname){    
  22. i=(i==(songslen-1))?1:i+2;    
  23. sgname=$('.songs_list li:nth-child('+i+')').text(); //音樂播放完畢之后切換下一首音樂    
  24. $('#gc ul').empty(); //清空歌詞    
  25. $('#aud').attr('src','music/'+sgname+'.mp3');    
  26. fn(sgname);    
  27. return;    
  28. }    
  29. }    
  30. }    
  31. };    
  32. });    
  33. } fn($('.songs_list li:nth-child(1)').text());   

那么到了這里你的音樂歌詞已經(jīng)能夠正常的同步顯示在頁面上了。不過還缺少一個東西,就是一個音樂的列表,我希望能夠點擊這個列表里的音樂,從而播放該音樂,下面附上代碼。
HTML代碼

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="songs_cnt">    
  2. <ul class="songs_list">    
  3. <li>Yesterday Once More</li>    
  4. <li>You Are Beautiful</li>    
  5. </ul>    
  6. <button class="sel_song"><br/><br/></button>    
  7. </div>    
  8. <div id="gc">    
  9. <ul></ul>    
  10. </div>   

css代碼

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. #gc{    
  2. width: 400px;    
  3. height: 400px;    
  4. background: transparent;    
  5. margin: 100px auto;    
  6. color: #fff;    
  7. font-size: 18px;    
  8. overflow: hidden;    
  9. position: relative;    
  10. }    
  11. #gc ul{    
  12. position: absolute;    
  13. top: 200px;    
  14. }    
  15. #gc ul li{    
  16. text-align: center;    
  17. height: 40px;    
  18. line-height: 40px;    
  19. }    
  20. .songs_cnt{    
  21. float: left;    
  22. margin-top: 200px;    
  23. position: relative;    
  24. }    
  25. .songs_list{    
  26. background-color: rgba(0,0,0,.2);    
  27. border-radius: 5px;    
  28. float: left;    
  29. width: 250px;    
  30. padding: 15px;    
  31. margin-left: -280px;    
  32. }    
  33. .songs_list li{    
  34. height: 40px;    
  35. line-height: 40px;    
  36. font-size: 16px;    
  37. color: rgba(255,255,255,.8);    
  38. cursor: pointer;    
  39. }    
  40. .songs_list li:hover{    
  41. font-size: 20px;    
  42. color: rgba(255,23,140,.6);    
  43. }    
  44. .sel_song{    
  45. position: absolute;    
  46. top: 50%;    
  47. width: 40px;    
  48. height: 80px;    
  49. margin-top: -40px;    
  50. font-size: 16px;    
  51. text-align: center;    
  52. background-color: transparent;    
  53. border: 1px solid #2DCB70;    
  54. font-weight: bold;    
  55. cursor: pointer;    
  56. border-radius: 3px;    
  57. font-family: sans-serif;    
  58. transition:all 2s;    
  59. -moz-transition:all 2s;    
  60. -webkit-transition:all 2s;    
  61. -o-transition:all 2s;    
  62. }    
  63. .sel_song:hover{    
  64. color: #fff;    
  65. background-color: #2DCB70;    
  66. }    
  67. .songs_list li.active{    
  68. color: #f00;    
  69. }   

js代碼

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. $('.songs_list li:nth-child(1)').addClass('active');    
  2. $('.songs_cnt').mouseenter(function () {    
  3. var e=event||window.event;    
  4. var tage.target||e.srcElement;    
  5. if(tag.nodeName=='BUTTON'){    
  6. $('.songs_list').animate({'marginLeft':'0px'},'slow');    
  7. }    
  8. });    
  9. $('.songs_cnt').mouseleave(function () {    
  10. $('.songs_list').animate({'marginLeft':'-280px'},'slow');    
  11. });    
  12. $('.songs_list li').each(function () {    
  13. $(this).click(function () {    
  14. $('#aud').attr('src','music/'+$(this).text()+'.mp3');    
  15. $('#gc ul').empty();    
  16. fn($(this).text());    
  17. $('.songs_list li').removeClass('active');    
  18. $(this).addClass('active');    
  19. });    
  20. })  

好了,到了這里,那么你的這個歌詞同步的效果的一些功能差不多都有了,關(guān)于HTML5使用Audio標(biāo)簽實現(xiàn)歌詞同步的效果今天也就到這里了。更多信息請登錄腳本之家網(wǎng)站了解更多!

標(biāo)簽:林芝 萍鄉(xiāng) 延邊 赤峰 保定 大同 泰州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5使用Audio標(biāo)簽實現(xiàn)歌詞同步的效果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    洞头县| 樟树市| 宜兰市| 常德市| 达日县| 碌曲县| 青河县| 社会| 镇巴县| 孟村| 安岳县| 双柏县| 白河县| 凤庆县| 大余县| 阜南县| 缙云县| 大港区| 麻栗坡县| 交口县| 雅江县| 宝应县| 新民市| 宜都市| 嵊州市| 八宿县| 丰镇市| 遂川县| 星子县| 巫溪县| 金川县| 鲁山县| 津市市| 安庆市| 桐庐县| 师宗县| 金川县| 保定市| 尖扎县| 无锡市| 古浪县|