欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

  發(fā)布時(shí)間:2016-03-17 14:04:06   作者:佚名   我要評(píng)論
HTML5的最強(qiáng)大之處莫過(guò)于對(duì)媒體文件的處理,如利用一個(gè)簡(jiǎn)單的vedio標(biāo)簽就可以實(shí)現(xiàn)視頻播放。類(lèi)似地,在HTML5中也有對(duì)應(yīng)的處理音頻文件的標(biāo)簽,那就是audio標(biāo)簽。通過(guò)本文給大家介紹HTML5使用Audio標(biāo)簽實(shí)現(xiàn)歌詞同步的效果,感興趣的朋友一起學(xué)習(xí)吧
HTML5的最強(qiáng)大之處莫過(guò)于對(duì)媒體文件的處理,如利用一個(gè)簡(jiǎn)單的vedio標(biāo)簽就可以實(shí)現(xiàn)視頻播放。類(lèi)似地,在HTML5中也有對(duì)應(yīng)的處理音頻文件的標(biāo)簽,那就是audio標(biāo)簽
HTML5出來(lái)這么久了,但是關(guān)于它里面的audio標(biāo)簽也就用過(guò)那么一次,當(dāng)然還僅僅只是把這個(gè)標(biāo)簽插入到了頁(yè)面中。這次呢就剛好趁著幫朋友做幾個(gè)頁(yè)面,拿這個(gè)audio標(biāo)簽來(lái)練練手。
首先你需要向頁(yè)面中插入一個(gè)audio標(biāo)簽,注意這里最好不要設(shè)置loop='loop',這個(gè)屬性使用來(lái)設(shè)置循環(huán)播放的,因?yàn)榈胶竺嫘枰褂胑nded屬性的時(shí)候,如果loop被設(shè)置為loop的話(huà),ended屬性將一直是false。
autoplay='autoplay'設(shè)置頁(yè)面加載后自動(dòng)播放音樂(lè),preload和autoplay屬性的作用是一樣的,如果標(biāo)簽中出現(xiàn)了autoplay屬性,那么preload屬性將被忽略。
controls='controls'設(shè)置顯示音樂(lè)的控制條。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <audio src="music/Yesterday Once More.mp3" id="aud" autoplay="autoplay" controls="controls" preload="auto">    
  2. 您的瀏覽器不支持audio屬性,請(qǐng)更換瀏覽器在進(jìn)行瀏覽。    
  3. </audio>   

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


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

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

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

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ā)生改變時(shí)觸發(fā)    
  9. for (var i = 0l = str.length; i < l; i++) {    
  10. if (this.currentTime /*當(dāng)前播放的時(shí)間*/ > str[i][0]) {    
  11. //顯示到頁(yè)面    
  12. $('#gc ul').css('top',-i*40+200+'px'); //讓歌詞向上移動(dòng)    
  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)前播放的音樂(lè)是否播放完畢    
  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(); //音樂(lè)播放完畢之后切換下一首音樂(lè)    
  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());   

那么到了這里你的音樂(lè)歌詞已經(jīng)能夠正常的同步顯示在頁(yè)面上了。不過(guò)還缺少一個(gè)東西,就是一個(gè)音樂(lè)的列表,我希望能夠點(diǎn)擊這個(gè)列表里的音樂(lè),從而播放該音樂(lè),下面附上代碼。
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">點(diǎn)<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. })  

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

相關(guān)文章

最新評(píng)論