HTML5 is most powerful in processing media files. For example, you can use a simple vedio tag to play a video. Similarly, there is a corresponding tag for processing audio files in HTML5, that is, the audio tag. This article describes how HTML5 uses the Audio tag to synchronize the lyrics. If you are interested, you can learn about it. The most powerful feature of HTML5 is the processing of media files, for example, you can use a simple vedio tag to play a video. Similarly, there is a corresponding tag for processing audio files in HTML5, that is, the audio tag.
HTML5 has been used for so long, but the audio tag in it has been used once. Of course, this tag is just inserted into the page. This time, I just helped my friends build several pages and use this audio tag to train my hands.
First, you need to insert an audio tag to the page. Note that loop = 'loop 'should not be set here. This attribute is used to set loop playback, this is because when the ended attribute needs to be used later, if the loop is set to loop, the ended attribute will always be false.
Autoplay = 'autoplay' sets the automatic playing of music after page loading. The preload and autoplay attributes play the same purpose. If the autoplay attribute appears in the tag, the preload attribute is ignored.
Controls = 'controls' sets the control bar for displaying music.
Copy XML/HTML Code to clipboard
-
- Your browser does not support the audio attribute. Change the browser to browse.
-
With this tag, congratulations! You can play music on your page. But will this make the page seem 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 be selected. First, we need to download some lrc-format lyrics, and then you need to format these music. Because this is the first music file.
We need to insert each lyrics into a two-digit array. After formatting, the lyrics will become in this format.
The code for formatting the lyrics is attached here.
Copy XML/HTML Code to clipboard
- // Synchronization of the lyrics
- Function parseLyric (text ){
- // Separate the text into one row and save it to an array
- Var lines = text. split ('\ n '),
- // The regular expression used to match the time. The matching result is similar to [xx: xx. xx].
- Pattern =/\ [\ d {2 }:\ d {2}. \ d {2} \]/g,
- // Save the final result Array
- Result = [];
- // Remove rows without time
- While (! Pattern. test (lines [0]) {
- Lineslines = lines. slice (1 );
- };
- // When '\ n' is used to generate an array, the last empty element in the result is removed.
- Lines [lines. length-1]. length = 0 & lines. pop ();
- Lines. forEach (function (v/* array element value */, I/* element index */, a/* array itself */){
- // Extract time [xx: xx. xx]
- Var time = v. match (pattern ),
- // Extract the lyrics
- Vvalue = v. replace (pattern ,'');
- // Because a row may have multiple times, the time may be [xx: xx. xx] [xx: xx. xx] [xx: xx. xx] format, which needs to be further separated
- Time. forEach (function (v1, i1, a1 ){
- // Remove the brackets in the time To get xx: xx. xx
- Var t = v1.slice (1,-1). split (':');
- // Press the result into the final Array
- Result. push ([parseInt (t [0], 10) * 60 + parseFloat (t [1]), value]);
- });
- });
- // Finally, sort the elements in the result array by time, so that the lyrics can be displayed normally after they are saved.
- Result. sort (function (a, B ){
- Return a [0]-B [0];
- });
- Return result;
- }
Here we can easily use the lyrics of each music. We need a function to get the lyrics and make them synchronously displayed on the page, switch the music normally. The code is attached below.
Copy XML/HTML Code to clipboard
- Function fn (sgname ){
- $. Get ('music/'+ sgname +'. lrc ', function (data ){
- Var str = parseLyric (data );
- For (var I = 0, li; I
- Li = $ ('
- '+ Str [I] [1] +'
');
- $ ('# Gc ul'). append (li );
- }
- $ ('# Aud') [0]. ontimeupdate = function () {// triggered when the current playback position of the Video audio changes
- For (var I = 0, l = str. length; I <l; I ++ ){
- If (this. currentTime/* Current playback time */> str [I] [0]) {
- // Display to the page
- $ ('# Gc ul'0000.css ('top',-I * 40 + 200 + 'px'); // move the lyrics up
- $ ('# Gc ul li'0000.css ('color',' # fff ');
- $ ('# Gc ul li: nth-child('{( I }1}'{'}.css ('color', 'red'); // highlight which lyrics are currently played
- }
- }
- If (this. ended) {// determines whether the current music has been played.
- Var songslen = $ ('. songs_list li'). length;
- For (var I = 0, val; I
- Val = $ ('. songs_list li: nth-child (' + (I + 1) + '). text ();
- If (val = sgname ){
- I = (songslen-1 ))? 1: I + 2;
- Sgname = $ ('. songs_list li: nth-child (' + I + '). text (); // switch to the next music after the music is played.
- $ ('# Gc ul'). empty (); // clear the lyrics
- $ ('# Aud'). attr ('src', 'music/'{sgname='hangzhou ');
- Fn (sgname );
- Return;
- }
- }
- }
- };
- });
- } Fn ($ ('. songs_list li: nth-child (1)'). text ());
Now, your music lyrics can be normally displayed on the page. However, there is still something missing, that is, a music list. I want to click the music in this list to play the music. The code is attached below.
HTML code
Copy XML/HTML Code to clipboard
-
- Yesterday Once More
- You Are Beautiful
- Point
Me
-
-
-
Css code
Copy XML/HTML Code to clipboard
- # 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,. 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 (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 2 s;
- -Moz-transition: all 2 s;
- -Webkit-transition: all 2 s;
- -O-transition: all 2 s;
- }
- . Sel_song: hover {
- Color: # fff;
- Background-color: #2DCB70;
- }
- . Songs_list li. active {
- Color: # f00;
- }
Js Code
Copy XML/HTML Code to clipboard
- $ ('. Songs_list li: nth-child (1)'). addClass ('active ');
- $ ('. Songs_cnt'). mouseenter (function (){
- Var e = event | window. event;
- Var tag = e.tar get | e. srcElement;
- If (tag. nodeName = 'button '){
- $ ('. Songs_list'). animate ({'marginleft': '0px '}, 'low ');
- }
- });
- $ ('. Songs_cnt'). mouseleave (function (){
- $ ('. Songs_list'). animate ({'marginleft': '-280px'}, 'low ');
- });
- $ ('. Songs_list li'). each (function (){
- $ (This). click (function (){
- $ ('# Aud'). attr ('src', 'music/'{}(this}.text ');
- $ ('# Gc ul'). empty ();
- Fn ($ (this). text ());
- $ ('. Songs_list li'). removeClass ('active ');
- $ (This). addClass ('active ');
- });
- })
Now, some of the functions of your lyrics synchronization are available. The effect of using the Audio tag to synchronize the lyrics in HTML5 is now here. For more information, log on to the script home website!