interface layout, horizontal direction four buttons evenly distributed, use weight, define android:layout_width="0DP", define layout_weight="1" This attribute is all consistent is evenly distributed
Gets the file object, using the new file () to implement the
Determines whether a file exists , the exists () method of The Files object , the length () must be greater than 0
Get MediaPlayer object, come out with new
Start playback
Call the Setaudiostreamtype () method of the MediaPlayer Object , set the audio stream type, parameters:audiomanager.stream_ MUSIC
Call the Setdatasource () method of the MediaPlayer Object , set the data source, parameter:String File path, or it can be an http
Call the prepare () method of the MediaPlayer object to prepare
Call the start () method of the MediaPlayer object to play
Call the play button's setenable () method, set the button as not clickable, parameter:false
When you have finished playing, you should set the button to click
Call the Setoncompletionlistener () method of the MediaPlayer Object , set the completion listener, parameters: The Oncompletionlistener interface, using an anonymous inner class implementation interface, overrides the method oncompletion () method, in this method calls the play button's Setenable (True)
Stop playing
Determines that the MediaPlayer object is not empty and that the MediaPlayer object isplaying () is True
Call the Stop () method of the MediaPlayer object to stop playback
Call the release () method of the MediaPlayer object , whether
Set the MediaPlayer object to null
Re-play
Determines that the MediaPlayer object is not empty and that the MediaPlayer object isplaying () is True
Call the MediaPlayer object seekto () method, set the playback progress, parameters:0 can
If it does not exist, then call the play function above directly
Pause Playback
Determines that the MediaPlayer object is not empty and that the MediaPlayer object isplaying () is true
Call the pause () method of the MediaPlayer object to pause playback
Call the SetText () method of the pause button to set the text to: Continue
In this click event to determine whether the text is "continue", if it is, continue to play
Call the start () method of the MediaPlayer Object
Call pause Anne's setText () method, set Text to: Pause
Mainactivity.java
PackageCom.tsh.tshplayer;ImportJava.io.File;Importjava.io.IOException;Importandroid.app.Activity;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.media.AudioManager;ImportAndroid.media.MediaPlayer;ImportAndroid.media.MediaPlayer.OnCompletionListener;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateEditText Et_music; PrivateButton Bt_play, Bt_pause, Bt_stop, Bt_replay; PrivateMediaPlayer player; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_music=(EditText) Findviewbyid (r.id.et_music); Bt_play=(Button) Findviewbyid (R.id.bt_play); Bt_pause=(Button) Findviewbyid (r.id.bt_pause); Bt_stop=(Button) Findviewbyid (r.id.bt_stop); Bt_replay=(Button) Findviewbyid (R.id.bt_replay); Bt_play.setonclicklistener ( This); Bt_pause.setonclicklistener ( This); Bt_stop.setonclicklistener ( This); Bt_replay.setonclicklistener ( This); } //Click events@Override Public voidOnClick (View v) {Switch(V.getid ()) {//Play CaseR.id.bt_play:play (); Break; //Pause CaseR.id.bt_pause:pause (); Break; //Replay CaseR.id.bt_replay:replay (); Break; //Stop CaseR.id.bt_stop:stop (); Break; } } //Stop playing Private voidStop () {if(player!=NULL&&player.isplaying ()) {player.stop (); Bt_play.setenabled (true); } } //Replay Music Private voidReplay () {if(player!=NULL&&player.isplaying ()) {Player.seekto (0); } } //Pause Music Private voidpause () {if(player!=NULL&&player.isplaying ()) {Player.pause (); Bt_pause.settext (Continue to); }Else if(Bt_pause.gettext (). toString (). Trim (). Equals ("Continue") ) {Player.start (); Bt_pause.settext (Pause); } } //Play Music Private voidPlay () {String music=Et_music.gettext (). toString (). Trim (); File File=NewFile (music); if(File.exists () && file.length () > 0) { Try{player=NewMediaPlayer (); Player.setaudiostreamtype (Audiomanager.stream_music); Player.setdatasource (music); Player.prepare (); Player.start (); //Set ButtonBt_play.setenabled (false); //Music Completion CallbackPlayer.setoncompletionlistener (NewOncompletionlistener () {@Override Public voidOncompletion (MediaPlayer MP) {bt_play.setenabled (true); } }); } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "Play Music Failed", 0). Show (); } } Else{Toast.maketext ( This, "Music file does not exist", 0). Show (); } }}
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "${relativepackage}.${activityclass}" > <EditTextAndroid:id= "@+id/et_music"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Music Path" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"> <ButtonAndroid:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Play"Android:id= "@+id/bt_play"/> <ButtonAndroid:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Paused"Android:id= "@+id/bt_pause"/> <ButtonAndroid:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Replay"Android:id= "@+id/bt_replay"/> <ButtonAndroid:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Stop"Android:id= "@+id/bt_stop"/> </LinearLayout></LinearLayout>
[Android] multimedia playback API Introduction