Synchronize the lyrics of the android mobile music player

Source: Internet
Author: User

Synchronize the lyrics of the android mobile music player

I recently learned a lot from a music player on an android phone, such as Fragment and ActionBar. Here I will first introduce the implementation of lyrics synchronization.

The idea of implementing lyrics synchronization is simple: Get the time and the content of the lyrics in the LRC file, and then play the corresponding content within the specified time. It is not difficult to obtain the lyrics. The difficulty lies in how to scroll the lyrics on the mobile phone screen.

First:

 

Start by reading the most basic lyrics file:

Public class LrcHandle {

Private List mWords = new ArrayList ();

Private List mTimeList = new ArrayList ();

// Process the lyrics File

Public void readLRC (String path ){

File file = new File (path );

Try {

FileInputStream fileInputStream = new FileInputStream (file );

InputStreamReader inputStreamReader = new InputStreamReader (

FileInputStream, "UTF-8 ");

BufferedReader bufferedReader = new BufferedReader (

InputStreamReader );

String s = "";

While (s = bufferedReader. readLine ())! = Null ){

AddTimeToList (s );

If (s. indexOf ("[ar :")! =-1) | (s. indexOf ("[ti :")! =-1)

| (S. indexOf ("[:")! =-1 )){

S = s. substring (s. indexOf (":") + 1, s. indexOf ("]");

} Else {

String ss = s. substring (s. indexOf ("["), s. indexOf ("]") + 1 );

S = s. replace (ss ,"");

}

MWords. add (s );

}

BufferedReader. close ();

InputStreamReader. close ();

FileInputStream. close ();

} Catch (FileNotFoundException e ){

E. printStackTrace ();

MWords. add ("no lyrics file, download it now ");

} Catch (IOException e ){

E. printStackTrace ();

MWords. add ("no lyrics are read ");

}

}

Public List getWords (){

Return mWords;

}

Public List getTime (){

Return mTimeList;

}

// Extract time

Private int timeHandler (String string ){

String = string. replace (".",":");

String timeData [] = string. split (":");

// Separate the output and seconds and convert them to an integer type.

Int minute = Integer. parseInt (timeData [0]);

Int second = Integer. parseInt (timeData [1]);

Int millisecond = Integer. parseInt (timeData [2]);

// Calculate the time for converting the previous row to the next row in milliseconds

Int currentTime = (minute * 60 + secondd) * 1000 + millisecond * 10;

Return currentTime;

}

Private void addTimeToList (String string ){

Matcher matcher = Pattern. compile (

"\ [\ D {1, 2 }:\\ d {1, 2} ([\.:] \ d {1, 2 })? \] "). Matcher (string );

If (matcher. find ()){

String str = matcher. group ();

MTimeList. add (new LrcHandle (). timeHandler (str. substring (1,

Str. length ()-1 )));

}

}

}

Generally, the format of the lyrics file is as follows:

[Ar: artist name]

[Ti: Qu name]

[Al: album name]

[By: Editor (the person who edits the LRC lyrics)]

[Offset: time compensation value] The unit is millisecond. A positive value indicates the overall advance, and a negative value indicates the opposite. This is used to adjust the overall display speed.

But not necessarily, sometimes there are no front ar: and other identifiers, so we also provide another resolution method here.

The time format in the lyrics file is relatively unified. 50] and so on. 00: Indicates minute, 00. indicates the number of seconds ,. 50 indicates the number of milliseconds. Of course, it is convenient to convert them into milliseconds.

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.