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.