(Original) C # LRC repeater-2

Source: Internet
Author: User

With a single English string, we can get the entire English string array, as shown below:

Public list <string> get the phrase list () {list <string> phrasecontent = new list <string> (); // get the phrase list and time is a bit different, because the loop will take one more for (INT I = 0; I <LRC file content. length; I ++) {If (get matching English phrase (LRC file content [I]) = NULL) // no matching item found, continue {continue ;} else {phrasecontent. add (obtain matching English phrases (LRC file content [I]);} return phrasecontent ;}

The cycle here is different from the acquisition time. I believe the viewer can understand why.

 

How can I obtain the file content? LRC is a text file. You can use file. readalllines provided by C # To get all the lines:

Private string [] Read the LRC file (string media file name) {// change the file name suffix to LRC to get a new file name. // If indexof is used, there may be two. this causes a fault // you need to use the last position matching the int point = media file name. lastindexof ('. '); string LRC file name = media file name. replace (media file name. substring (position at the vertex + 1, 3), "LRC"); If (file. exists (media file name) & file. exists (LRC file name) {try {return file. readalllines (LRC file name);} catch (exception) {string output = string. format ("{0} file read error", LRC file name); throw new exception (output ) ;}} Else {string output = string. Format ("{0} or {1} does not exist! ", Media file name, LRC file name); throw new exception (output );}}
Public LRC (string media file name) {This. LRC file content = read LRC file (media file name );}

This program requires that the media file to be played be the same as the LRC file name.

The constructor contains the content of this. LRC file, which is a string array. All time pairs and phrases are based on it.

Okay. So far, a class that reads LRC and can generate time pairs and strings from it is complete. Now we can use it with WMP.

Let's take a look at how to play and stop some clips in the sound:

Private void button play _ click (Object sender, eventargs e) {// axwindowsmediaplayer1.ctlcontrols. currentposition = 11.01; 11.01 seconds // The value of currentposition indicates the time at which you want to play axwindowsmediaplayer1.ctlcontrols with the following clock. currentposition = start time; axwindowsmediaplayer1.ctlcontrols. play ();} private void timereffectick (Object sender, eventargs e) {var millisecond time = get Millisecond (axwindowsmediaplayer1); // you need to use> = to determine whether to use this parameter, if (millisecond> = End Time) {axwindowsmediaplayer1.ctlcontrols. pause ();} // keep displaying the current playback time label1.text = Millisecond Time. tostring ();}
Private Static double get Millisecond (axwindowsmediaplayer media control) {// directly return the current time is also OK // return media control. ctlcontrols. currentposition; // However, only two digits after the decimal point are allowed for display. // For example, 3.45986*100 = 345.986 // math. floor (345.986) converts the minimum integer to 345 // divide it by 100 to 3.45, and then returns math without rounding. floor (media control. ctlcontrols. currentposition * 100)/100; // if you want to get three digits, you can do this: // return math. floor (media control. ctlcontrols. currentposition * 1000)/1000 ;}

It mainly uses several of the most common clip playback controls in WMP.

Ctlcontrols. currentposition, starting from the time you specified, followed by ctlcontrols. Play (); will play continuously. If you do not set the end time, it will play until the media file ends.

A timer control is used to control the end. The timer will be executed every 10 milliseconds, note that the initial state is disabled (Enabled = false), because the program can only start control after reading the LRC file, so you must first close it.

Ctlcontrols. Pause is paused. At the beginning, I used ctlcontrols. Stop, and the result start time is changed to 0. This is a note.

Now we can control the clip playback. How can we get the correct time? ListBox and the list of obtained phrases are used.

Select a media file:

Private void button select file _ click (Object sender, eventargs e) {// all initialization starts after reading the LRC file! Openfiledialog1.filter = "music file (*. MP3 )(*. WMA) | *. MP3 ;*. WMA | all files (*. *) | *. * "; if (openfiledialog1.showdialog () = dialogresult. OK) {axwindowsmediaplayer1.url = openfiledialog1.filename; LRC mylrc = New LRC (openfiledialog1.filename); time pair list = mylrc. get the time pair list (); // read all the English phrases to a ListBox phrase list = mylrc. get the phrase list () and ListBox phrase list. datasource = phrase list; // manually called to read aloud at the beginning! Play all the buttons. optional mclick (); button. visible = true; next sentence of the button. visible = true; button playback. visible = true; // The start clock is the stopped timer1.start ();}}

In the form, I first defined several variables.

Private Double Start time; private double end time; private list <time pair> time pair list; private list <string> phrase list;

Currently, the ListBox contains text display. When you click an item in ListBox, the program will get the time pair and use a selectedindexchanged event in ListBox for processing.

 
Private void listbox1_selectedindexchanged (Object sender, eventargs E) {// when loading the listbox1 content at the beginning, the time pair list may be blank. // assume that the obtained time pair list and phrase list are in the same order. // If the LRC is not standard inconsistency may occur // because they are obtained separately if (Time pair list! = NULL) {start time = Time pair list [ListBox phrase list. selectedindex]. start time; End Time = Time pair list [ListBox phrase list. selectedindex]. end Time;}. For example, when you click the second row, the program will obtain the ListBox phrase list. the value of selectedindex is 1, and because my time pair list is a list <time pair>, theoretically they correspond one to one, so they can be accessed by subscript, in this way, the corresponding start time and end time can be obtained,

When you click to play the video, the video will be played according to the time of the sentence you selected.

OK. The core functions have been completed. Let's handle some control details.

To be continued...

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.