Many netizens are very interested in the MP3 player I posted before. So I am going to write a tutorial to introduce some functions in detail...
Principle:
In fact, the lrc file is just a text document in a fixed format.
By
[Time] Lyrics
Or
[Time] [time...] Lyrics
Save in this form
Split the line with a linefeed, and then split the lyrics and time. Save the lyrics and time to the array.
Code: check the code directly.
The code is not optimized, and the code is messy. You can use it directly when you are surprised:
Getlrc (the path of the lrc file is enough)
The lyrics will be saved in an array named newlrc. Of course, you can change it to a returned array, so that you can use them as needed.
Note: swf cannot read external files across domains (learn more about security> About domains, cross-domain security, and SWF files)
By default, Flash Player 7 and later versions prevent reading data, objects, or variables from SWF files provided by one domain. In addition, content loaded through insecure (non-HTTPS) protocols cannot be read, even if both are in the same domain. For example, the SWF file located in the http://www.macromedia.com/main.swf cannot load data in the https://www.macromedia.com/data.txt without explicit permission; the SWF file provided from one domain cannot load data in another domain (for example, usingLoadVars ()
).
To solve this problem, you can use dynamic language (.. net/asp/php/jsp. or directly store the lyrics in your own space.
Now, let's talk about the lyrics. Next we will introduce the synchronization between the lyrics and mp3...
// Analyze the lyrics
Function getlrc (path ){
// Define two arrays. mylrc uses. newlrc as the final array to save the lyrics during analysis.
Newlrc = new Array ();
Mylrc = new Array ();
// Load the external lrc file
Lrc = new LoadVars ();
Lrc. load (path );
Lrc. onData = function (str ){
// Determine whether the load is successful
If (str! = Undefined ){
// Use line breaks to separate the lyrics and save them in mylrc
Mylrc = str. split (chr (13) + chr (10 ));
// Loop each row...
For (I = 0; I <mylrc. length; I ++ ){
If (mylrc [I]. substr (0, 3) = "[ti "){
// If "[ti" is the song name, it is not required for the time being. Leave a note. The same below //
// Var temp = mylrc [I]. split (":") [1];
// Temp = temp. substr (0, temp. length-1 );
// Newlrc. push ("00:00. 00 | song name:" + temp );
} Else if (mylrc [I]. substr (0, 3) = "[ar "){
// Singing //
// Var temp = mylrc [I]. split (":") [1];
// Temp = temp. substr (0, temp. length-1 );
// Newlrc. push ("00:00. 01 | singing:" + temp );
} Else if (mylrc [I]. substr (0, 3) = "[al "){
// Album //
// Var temp = mylrc [I]. split (":") [1];
// Temp = temp. substr (0, temp. length-1 );
// Newlrc. push ("00:00. 02 | album:" + temp );
} Else if (mylrc [I]. substr (0, 1) = "["){
// Normal lyrics
Var temp: Array = mylrc [I]. split ("]"); // determine whether there are two or more times at the same time (sometimes this happens when a lyrics appear several times)
If (temp. length = 2) {// There is only one Lyrics
Newlrc. push (deleteStr (temp [0]) + "|" + temp [1]); // add an array
} Else if (temp. length> 2) {// There are more than two lyrics
For (k = 0; k <temp. length-1; k ++ ){
Newlrc. push (deleteStr (temp [k]) + "|" + temp [temp. length-1]); // add an array
}
}
// Ps: There are two ways to save the array of lyrics:
// Newlrc ["00.00.0"] = "lyrics"
// Newlrc [0] = "00.00.0 | lyrics"
// When only one line of lyrics is displayed, the first method is more convenient.
// For the list display, the second is more convenient
}
}
}
If (newlrc. length <1) {// loading failed or analysis failed.
Newlrc. push ("00.00.0 | no lyrics found ");
}
Newlrc. push ("99.99.9 | welcome to i4cd mp3player ");
Newlrc. sort (); // sort the time. If you only need to display one by one, you can also
// The following code only shows the lyrics, not necessary
TxtArea. text = "";
For (I = 0; I <newlrc. length; I ++ ){
TxtArea. text + = newlrc [I]. split ("|") [1] + "\ n ";
}
};
}
Function deleteStr (str) {// delete redundant []
Var temp: String = str;
Return (temp. substr (1, temp. length-1 ));
}
Function getStr (str, num) {// return the time text in the specified format
Var temp: String = str;
If (temp. length <num ){
Temp = temp + ". 00 ";
}
Return (temp. substr (0, num ));
}