Android music player (prototype)

Source: Internet
Author: User

Ding Lang suggestion: before learning this instance, Please master the Activity lifecycle related events and methods, so that the learning effect will be better.
 
This instance is for reference only and is not a perfect product. Due to time and technical limitations, we apologize for any shortcomings or errors. Hope enthusiastic netizens can continue to improve.
Below is part of the Activity code (I usually have detailed comments ):

Package cn. chaoyang. activity;
 
Import java. io. File;
Import java. io. IOException;
 
Import android. app. Activity;
Import android. media. MediaPlayer;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. text. BoringLayout. Metrics;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
// Before learning this instance, Please master the Activity lifecycle and related methods, so that the learning effect will be better.
Public class MainActivity extends Activity {
Private MediaPlayer mediaplayer;
Private EditText txtName;
Private int postion;
Private String fileName;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ButtonClickListener listener = new ButtonClickListener ();
TxtName = (EditText) this. findViewById (R. id. inputName );
Button btnPlay = (Button) this. findViewById (R. id. btnPlay );
Button btnPause = (Button) this. findViewById (R. id. btnPause );
Button btnStop = (Button) this. findViewById (R. id. btnStop );
Button btnResart = (Button) this. findViewById (R. id. btnRestart );
BtnPlay. setOnClickListener (listener );
BtnPause. setOnClickListener (listener );
BtnStop. setOnClickListener (listener );
BtnResart. setOnClickListener (listener );
}
// After the system recovers, you can read the previously saved Status values again.
@ Override
Protected void onRestoreInstanceState (Bundle savedInstanceState ){
This. fileName = savedInstanceState. getString ("fileName ");
This. postion = savedInstanceState. getInt ("postion ");
Super. onRestoreInstanceState (savedInstanceState );
}
// In the event of an accident, some status values are saved before the system kills the Activity process.
@ Override
Protected void onSaveInstanceState (Bundle outState ){
OutState. putString ("fileName", fileName );
OutState. putInt ("postion", postion );
Super. onSaveInstanceState (outState );
}
// The onDestroy method can kill the program process and completely release resources.
@ Override
Protected void onDestroy (){
Mediaplayer. release ();
Super. onDestroy ();
}
// If the call is over, continue playing the music
@ Override
Protected void onResume (){
If (postion> 0 & fileName! = Null)
{
Try {
Play ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Mediaplayer. seekTo (postion );
Postion = 0;
}
Super. onResume ();
}
// If you call or send a text message, Acticity will pause and stop playing music.
@ Override
Protected void onPause (){
If (mediaplayer. isPlaying ())
{
Postion = mediaplayer. getCurrentPosition (); // Save the current playback point
Mediaplayer. stop ();
}
Super. onPause ();
}
 
Private final class ButtonClickListener implements View. OnClickListener
{

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Mediaplayer = new MediaPlayer ();

Button button = (Button) v;
Try {
Switch (v. getId ())
{
// Play
Case R. id. btnPlay:
If (! Mediaplayer. isPlaying ())
{
Play ();
}
Break;
// Pause
Case R. id. btnPause:
// If the video is being played, press the button and pause. The text on the button is displayed as "continue".
If (mediaplayer. isPlaying ())
{
Mediaplayer. pause ();
Button.setText(R.string.txt Continue); // sets the button text
} Else {
// If it is paused, press the button to continue playing
// Play ();
}
Break;
// Stop
Case R. id. btnStop:
If (mediaplayer. isPlaying ()){
Mediaplayer. stop ();
}
Break;

// Repeated
Case R. id. btnRestart:
If (mediaplayer. isPlaying ()){
Mediaplayer. seekTo (0 );
} Else {
Play ();
}
Break;
}
} Catch (Exception e ){
// TODO: handle exception
}


}
 
}
Private void play () throws IOException
{
// Obtain the absolute path of the music file
FileName = txtName. getText (). toString ();
File file = new File (Environment. getExternalStorageDirectory (), fileName );
Mediaplayer. reset (); // reset
Mediaplayer. setDataSource (file. getAbsolutePath (); // you can specify the data source to be played.
Mediaplayer. prepare ();
Mediaplayer. start ();
}
}

The following is the software layout file code, which is a simple linear layout.

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/labName"
/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/inputName"
/>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/play"
Android: id = "@ + id/btnPlay"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/pause"
Android: id = "@ + id/btnPause"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/stop"
Android: id = "@ + id/btnStop"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/restart"
Android: id = "@ + id/btnRestart"
/>
</LinearLayout>
</LinearLayout>

The following is the string. xml code of the resource file.

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, MainActivity! </String>
<String name = "app_name"> mp3 player </string>
<String name = "labName"> enter the song name </string>
<String name = "play"> playback </string>
<String name = "pause"> pause </string>
<String name = "stop"> stop </string>
<String name = "restart"> repeated </string>
<String name = "txtContinue"> continue </string>
</Resources>

 
 
The purpose of this example is to familiarize yourself with the use of audio interfaces and related operations in android, and to consolidate the knowledge about the Activity lifecycle. As for the page layout, the display and control style is very silly.
If you want to develop a complete (music player-related) product, there are too many things to be improved.

From: A programmer in the programming world

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.