Android lyrics Synchronization

Source: Internet
Author: User

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 lyrics of the network 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

Here, I recommend the yoyoplayer open-source project for music playback. The source code already supports LRC resolution. It will be used when I take it.

The process is like this: Read the LRC file into the memory and store it in the sentence data structure. There are three member variables fromtime, totime, and content in sentence. This data is required for display.

Ii. LRC lyrics display

The ondraw method is rewritten to draw the lyrics.

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.

 

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.

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 a 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.

Private class myhandler extends handler {

@ Override

Public void handlemessage (Message MSG ){

// Log. V ("################# hahah", "" + MP. getcurrentposition ());

// Redraw

Lrcview. mlyric. settime (MP. getcurrentposition ());

Lrcview. invalidate ();

}

}

Iv. Acquisition of lyrics

The obtained lyrics are searched by 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 );

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.