search
HomeWeb Front-endH5 TutorialHTML5 uses the Audio tag to achieve the effect of lyrics synchronization _html5 tutorial skills

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 is a corresponding tag 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, and 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 loop playback, because when you need to use the ended attribute later, if loop is set to If looping, the ended attribute will always be false.
autoplay='autoplay' sets the page to automatically play music after loading. 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' sets the control bar for displaying music.

XML/HTML CodeCopy content to clipboard
  1. audio src="music/Yesterday Once More .mp3" id="aud" autoplay="autoplay" controls="controls" preload="auto"> 
  2. Your browser does not support the audio attribute, please change the browser to browse. 
  3. audio> 

After you have this tag, congratulations, your page can now 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 music file at the beginning looked like this


We need to insert each lyric into a two-digit array. After formatting, the lyrics will become like this.
Attached here is the code for formatting lyrics

XML/HTML CodeCopy content to clipboard
  1. //Lyrics synchronization part
  2. function parseLyric(text) {
  3. //Separate the text into line by line and store it in the array
  4. var lines = text.split('n'),
  5. //Regular expression used to match time, the matching result is similar to [xx:xx.xx]
  6. pattern = /[d{2}:d{2}.d{2}]/g,
  7. //Array to save the final result
  8. result = [];
  9. //Remove lines without time
  10. while (!pattern.test(lines[0])) {
  11. lineslines = lines.slice(1);
  12. };
  13. //When using 'n' to generate the array above, the last element in the result is an empty element, which will be removed here
  14. lines[lines.length - 1].length === 0 && lines.pop();
  15. lines.forEach(function(v /*array element value*/ , i /*element index*/ , a /*array itself*/ ) {
  16. //Extract time [xx:xx.xx]
  17. var time = v.match(pattern),
  18. //Extract lyrics
  19. vvalue = v.replace(pattern, '');
  20. //Because there may be multiple times in one line, time may be in the form of [xx:xx.xx][xx:xx.xx][xx:xx.xx], which needs to be further separated
  21. time.forEach(function(v1, i1, a1) {
  22. //Remove the square brackets in the time to get xx:xx.xx
  23. var t = v1.slice(1, -1).split(':');
  24. //Push the result into the final array
  25. result.push([parseInt(t[0], 10) * 60 parseFloat(t[1]), value]);
  26. });
  27. });
  28. //Finally, sort the elements in the result array by time so that the lyrics can be displayed normally after saving
  29. result.sort(function(a, b) {
  30. return a[0] - b[0];
  31. });
  32. return result;
  33. }

At this point we can easily use the lyrics of each piece of music. We need a function to obtain the lyrics and display them on the page synchronously, so that the music can be switched normally. The code is attached below.

XML/HTML CodeCopy content to clipboard
  1. function fn(sgname){
  2. $.get('music/' sgname '.lrc',function(data){  
  3. var str=parseLyric(data);
  4. for(var i=0,li;istr.length;i ){
  5. li=$('li>' str[i][1] 'li>'); 
  6. $('#gc ul').append(li);
  7. }
  8. $('#aud')[0].ontimeupdate=function(){//video Triggered when the current playback position of the audio changes
  9. for (var i = 0, l = str.length; i l; i ) {
  10. if (this.currentTime /*Current playback time*/ > str[i][0]) {
  11. //Display to page
  12. $('#gc ul').css('top',-i*40 200 'px'); //Move the lyrics up
  13. $('#gc ul li').css('color','#fff');
  14. $('#gc ul li:nth-child(' (i 1) ')').css('color','red'); //Highlight which lyric is currently being played
  15. }
  16. }
  17. if(this.ended){ //Determine whether the currently playing music has finished playing
  18. var songslen=$('.songs_list li').length;
  19. for(var i= 0,val;isongslen;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(); //Switch after the music is finished playing A piece of music
  24. $('#gc ul').empty(); //Clear lyrics
  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());
Now here your music lyrics can be displayed on the page normally and synchronously. But there is still one thing missing, which is a list of music. I hope to be able to click on the music in this list to play the music. The code is attached below.

HTML code

XML/HTML Code
Copy content to clipboard
  1. div class="songs_cnt">    
  2. ul class="songs_list">    
  3. li>Yesterday Once Moreli>    
  4. li>You Are Beautifulli>    
  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复制内容到剪贴板
  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复制内容到剪贴板
  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. })  

好了,到了这里,那么你的这个歌词同步的效果的一些功能差不多都有了,关于HTML5使用Audio标签实现歌词同步的效果今天也就到这里了。更多信息请登录脚本之家网站了解更多!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
html5的div一行可以放两个吗html5的div一行可以放两个吗Apr 25, 2022 pm 05:32 PM

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别是什么html5中列表和表格的区别是什么Apr 28, 2022 pm 01:58 PM

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

html5怎么让头和尾固定不动html5怎么让头和尾固定不动Apr 25, 2022 pm 02:30 PM

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有哪些html5中不支持的标签有哪些Mar 17, 2022 pm 05:43 PM

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了哪个列表标签html5废弃了哪个列表标签Jun 01, 2022 pm 06:32 PM

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是什么意思html5是什么意思Apr 26, 2021 pm 03:02 PM

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

html5为什么只需要写doctypehtml5为什么只需要写doctypeJun 07, 2022 pm 05:15 PM

因为html5不基于SGML(标准通用置标语言),不需要对DTD进行引用,但是需要doctype来规范浏览器的行为,也即按照正常的方式来运行,因此html5只需要写doctype即可。“!DOCTYPE”是一种标准通用标记语言的文档类型声明,用于告诉浏览器编写页面所用的标记的版本。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version