the principle of lyrics synchronization is actually very simple:
the lyrics are displayed in a thread, which synchronizes the display progress and playback progress of the lyrics during music playback.
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's top 13 excellent Chinese characters]
[: fell in love with you music network]
. 00] [00:38. 00] string your heart and my heart
. 00] [00:41. 00] Is it a lucky grass or a lucky grass? Concentric circles
[. 00] [. 00] Let all expectations? The call
[. 00] [. 00] When you are young? Companion
[. 00] [. 00] [. 00]? Let the younger the longer the more lonely
. 00] [02: 27. 00] [00: 56. 00] plant my lucky grass in your mengtian.
. 00] [02: 31. 00] [0: 01. 00] Let the earth follow me?
the LRC format is [start time of display of lyrics] [end time of display of lyrics] content of lyrics.
to understand the principle of lyrics synchronization, we can think of the following work:
1. LRC parsing
2. LRC lyrics
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.Source codeLRC Parsing is already well supported.
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.
DrawnCodePost it:
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.baidu.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 );
Original article: http://my.oschina.net/xiahuawuyu/blog/58228