The principle of lyrics synchronization is actually very simple:
The display of the lyrics is a thread. During music playback, a thread synchronizes the display progress of the lyrics with the playback progress.
The standard network lyrics are LRC. Let's take a look at the next LRC document, and its format will be clear at a glance.
[Ti: Love]
[Ar: Tigers]
[Al: Warner has 13 excellent Chinese characters]
[By: love your music network]
[. 00] [. 00] string your heart and my heart
[. 00] [. 00] A lucky grass, a string? Concentric circles
[. 00] [. 00] Let all expectations? Call
[. 00] [. 00] When you are young? Companion
[. 00] [. 00] [. 00]? Let the younger the longer the more lonely
[. 00] [. 00] [. 00] plant my lucky grass in your mengtian
[. 00] [. 00] [. 00] Let the earth accompany me? Concentric circles
The LRC format is [start time of the lyrics display] [end time of the lyrics display] The lyrics content.
To understand the principle of lyrics synchronization, we can think of the following work:
1. LRC Parsing
2. LRC lyrics display
3. Synchronization of lyrics and playing music
4. Obtain the lyrics
LRC Parsing
We recommend that you use the YOYOPlayer open-source program for music playback. The source code supports LRC parsing.
The process is like this: Read the LRC file into the memory and store it in the sentence data structure. Sentence contains three member variables: Fromtime, Totime, and content. This data is required for display.
2. LRC lyrics display
Draw the lyrics by rewriting the OnDraw method.
Paste the drawn code:
Long t = tempTime;
Int index = getNowSentenceIndex (t );
If (index =-1 ){
Return;
}
Sentence now = list. Get (index );
Float f = (t-now. GetFromTime () * 1.0f/(now. getToTime ()-now. getFromTime ());
If (f> 0.98f ){
F = 0.98f;
}
Shader shader = new LinearGradient (0, 0, now. GetContentWidth (mTxtPaint), 0, new int [] {Color. RED, Color. BLUE}, new float [] {f, f + 0.01f}, TileMode. CLAMP );
MTxtPaint. setShader (shader );
Canvas. drawText (now. getContent (), 0, 20, mTxtPaint );
The code above is very simple. The key function is getNowSentenceIndex (t). You can obtain the index of the lyrics through the playback time. Let's take a look at how getNowSentenceIndex () is implemented.
Java code:
Private int getNowSentenceIndex (long t ){
For (int I = 0; I <list. size (); I ++ ){
If (list. get (I). isInTime (t )){
Return I;
}
}
// Throw new RuntimeException ("It turns out that it cannot be found! ");
Return-1;
}
There is also a gradient effect of the lyrics. The key code is in the setting of the paint brush, as shown below.
Java code:
Shader shader = new LinearGradient (0, 0,
Now. getContentWidth (mTxtPaint), 0, new int [] {Color. RED, Color. BLUE}, new float [] {f, f + 0.01f}, TileMode. CLAMP );
MTxtPaint. setShader (shader );
3. Synchronization of lyrics and playing music
When playing the music, start the lyrics display thread at the same time. Set the time for playing the music to the member variable of the lyrics View for synchronization.
Java code:
Private class MyHandler extends Handler {
@ Override
Public void handleMessage (Message msg ){
// Log. v ("################# hahah", "" + mp. getCurrentPosition ());
// Redraw
LrcView. mLyric. setTime (mp. getCurrentPosition ());
LrcView. invalidate ();
}
}
4. Obtain the lyrics through Baidu. The specific code is as follows. The key code is as follows:
GetMethod get = new GetMethod ("http://www.baidu.com/s? Wd = "+ URLEncoder. encode (" filetype: lrc "+ key," GBK "));
Get. addRequestHeader ("Host", "www.2cto.com ");
Get. addRequestHeader ("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.8.1.11) Gecko/20071127 Firefox/2.0.0.11 ");
Get. addRequestHeader ("Accept", "text/xml, application/xml, application/xhtml + xml, text/html; q = 0.9, text/plain; q = 0.8, image/png, */*; q = 0.5 ");
Get. addRequestHeader ("Accept-Language", "zh-cn, zh; q = 0.5 ");
Get. addRequestHeader ("Keep-Alive", "300 ");
Get. addRequestHeader ("Referer", "http://www.baidu.com /");
Get. addRequestHeader ("Connection", "keep-alive ");
Int I = http.exe cuteMethod (get );
From Mr_Hannibal