Last week we had a completion project defense, which was the player project. There is a very interesting way to add lyrics to a player's singing side, like a cool dog, as the lyrics and songs are synchronized.
The tools here are Visual Studio 2013, which uses languages such as C # and. NET technology.
First we use the file.exists (@ "file path") to determine if the LRC lyrics file exists, and if so, read the LRC file.
You can use a regular expression here, or you can use a two-dimensional array or an array of two. This article is a two-dimensional array.
The LRC file is also a text file, and the lyrics are a line of text. Simple understanding is like DataReader in the database, one line of text (record), and then we use the substring method to intercept the time and lyrics within the text, and put it into the array.
This is probably the file.
After we read it, the player has a property,Ctlcontrols.currentPositionString:string; Gets the current progress, returning a string-formatted value. such as "00:23"
We get this value to match the value in the "Save Time" array to determine when media Player is playing = This line time, then let the line highlight. (Always refresh with timer, keep in sync)
In this way, you can achieve the same as a cool dog lyrics and songs synchronized roll-like.
Finally attach source code
1 #regionLyrics2 string[,] LRC =New string[2, -];//Save lyrics and current progress3 /// <summary>4 ///Read and display lyrics5 /// </summary>6 Public voidSHOWLRC ()7 {8 if( This. Axwindowsmediaplayer2.playstate = =WMPLib.WMPPlayState.wmppsPlaying)9 {Ten Try One { A //Using : The function is to automatically free up memory when finished - //StreamReader: The function is to read bytes from a byte stream with a specific encoding - using(StreamReader sr =NewStreamReader (Ktvutil.songpath +@"\"+ playlist.playingsongname () +". LRC", Encoding.default)) the { - String Line; - //Loop through each line of lyrics - while(line = Sr. ReadLine ())! =NULL) + { - //To store the read lyrics in an array + for(inti =0; I < -; i++) A { at if(lrc[0, I] = =NULL) - { -lrc[0, I] = line. Substring (Ten, line. Length-Ten); - Break; - } - } in //Store the Read lyrics time into an array - for(inti =0; I < -; i++) to { + if(lrc[1, I] = =NULL) - { thelrc[1, I] = line. Substring (1,5); * Break; $ }Panax Notoginseng } - } the /*********** Dynamic Display lyrics ***************/ + //get the current progress of the player A stringNUMSS = This. axWindowsMediaPlayer2.Ctlcontrols.currentPositionString; the for(inti =0; I < -; i++) + { - if(lrc[1, I]. Equals (NUMSS)) $ { $ This. Lbllrc.text = lrc[0, I]; - } - //Else the //{ - //this.lblLrc.Text = "************";Wuyi //} the } - } Wu } - Catch(Exception ex) About { $ //MessageBox.Show ("exception:" + ex.) Message); - } - } - } A /// <summary> + ///Refresh Lyrics the /// </summary> - /// <param name= "Sender" ></param> $ /// <param name= "E" ></param> the Private voidTimer5_tick (Objectsender, EventArgs e) the { the SHOWLRC (); the } - #endregion
PS. Everyone has good suggestions welcome to the present:)
C#winfrom player Dynamically loading lyrics