Simple Android music player

Source: Internet
Author: User

After the basic Android training is complete, you can create a simple music player. For the first small Android project, baseline the picture ~~~~

The UI is inspired by the fact that thunder looks at the player and plays the mediaplayer class when audio is used in Android.

The audio track control is more complex. The progress and audio track control uses seekbar and the custom style seekbar_img.xml.CodeAs follows:

 

< Layer-list Xmlns: Android = "Http://schemas.android.com/apk/res/android"   >

item
Android: ID =" @ + Android: ID/background "
Android: drawable = "@ drawable/bbar_bg" />
item
Android: ID =" @ + Android: ID/progress "
Android: drawable = "@ drawable/bbar" />

</Layer-list>

Audio Track Control:

 

< Layer-list Xmlns: Android = "Http://schemas.android.com/apk/res/android"   >

item
Android: ID =" @ + Android: ID/background "
Android: drawable = "@ drawable/bar_bg" />
item
Android: ID =" @ + Android: ID/progress "
Android: drawable = "@ drawable/Bar" />

</Layer-list>

It is a bit complicated to display the lyrics. However, if you carefully analyze the lyrics, you can find a solution. There is a corresponding time before each sentence in the lyrics file. You only need to calculate the current playback time,

Then, determine whether the match exists. If the match exists, use the I/O operation to read the corresponding lyrics from the lyrics file.

 

View code

/**
* Read the lyrics File
*/
Public Static String read (file path, string filename)
{
Stringbuilder sb = New Stringbuilder ();
Set <string> LRCS = fileutil. getlrcs (PATH );
// Get the lyrics file name
String lrcfile = filename. substring (0, filename. indexof ("."));
Try
{
Bufferedreader BR = New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (path + "/" + lrcfile + ". LRC"), "GBK "));
String con = "";
While (Con = Br. Readline ())! = Null )
{
SB. append (con + "\ n ");
}
BR. Close ();
} Catch (Exception E)
{
E. printstacktrace ();
}
Return SB. tostring ();

}

The next step is to convert the current playback time to hour, minute, and second,

 

View code

Public Static String formatms ( Int MS)
{
Int S = MS/1000; // Seconds
Int M = s/60; // Minute
Int Add = S % 60; // Seconds
String con = "";
If (Add> 10 & M> 10)
Con = m + ":" + Add;
Else If (Add <10 & M> 10)
Con = m + ": 0" + Add;
Else If (M <10 & add <10)
Con = "0" + M + ": 0" + Add;
Else If (M> 10 & add <10)
Con = "" + M + ": 0" + Add;
Else If (M <10 & add> 10)
Con = "0" + M + ":" + Add;
Return Con;
}

A subthread is used to track the playback progress and display the lyrics progress,

 

// Change the progress of the sub-thread monitoring
Private Runnable thread = New Runnable ()
{
@ Override
Public Void Run ()
{
// Update View
Updatetextview ();
// Automatically play the next one
Playnext ( True );
// Show lyrics
Showlrc ();
Handler. postdelayed (thread, 1000 );
}
};

Here is a brief introduction. Click to download the Code:

/Files/jaylong/Android Audio Recorder .zip

 

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.