Android phone music player to sync lyrics

Source: Internet
Author: User

Recently in making an Android phone on the music player, learned a lot of things, such as the use of Fragment,actionbar, and so on, here is the first to introduce the implementation of the lyrics synchronization.

The realization of the lyrics synchronization is simple: Get the time and lyrics in the lyrics file LRC, and then play the corresponding content within the specified time. The difficulty lies in how to achieve the scrolling of the lyrics on the mobile phone screen.

First up Effect chart:

Start with the most basic reading lyrics file:

public class Lrchandle {

Private List mwords = new ArrayList ();

Private List mtimelist = new ArrayList ();

Working with lyrics files

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 ("[By:")!=-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, hurry to download");

catch (IOException e) {

E.printstacktrace ();

Mwords.add ("No reading to lyrics");

}

}

Public List getwords () {

return mwords;

}

Public List GetTime () {

return mtimelist;

}

Isolate the time

private int Timehandler (string string) {

String = String.Replace (".", ":");

String timedata[] = String.Split (":");

Separate out minutes, seconds, and convert to integral type

int minute = Integer.parseint (timedata[0]);

int second = Integer.parseint (timedata[1]);

int millisecond = Integer.parseint (timedata[2]);

Converts the time between the previous line and the next line to the number of milliseconds

int currenttime = (minute + second) * 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));

}

}

}

The format of the general lyrics file is probably as follows:

[ar: Artist name]

[Ti: Qu name]

[Al: Album Name]

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

[Offset: Time-compensated value] is in milliseconds, and positive values indicate the overall advance and negative values. This is used to adjust the overall speed of the display.

But not necessarily, sometimes there is no previous ar: identifiers, so we also provide another way of parsing.

The time format in the lyrics file is more uniform: [00:00.50] and so on, 00: for minutes, 00 for seconds,. 50 for milliseconds, and, of course, it's more convenient to convert them to millisecond processing.

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.