C#winfrom player Dynamically loading lyrics

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.