The Main.xml program is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G T <textview android:id= "@+id/info" android:layout_width= "fill_parent" android:layout_height= "Wrap_co Ntent "android:text=" Waiting for audio files to play ... "/> <linearlayout android:layout_width=" Wrap_content "Andro id:layout_height= "wrap_content" android:orientation= "horizontal" > <imagebutton android:id= "@+id/play" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android oid:src= "@+drawable/play"/> <imagebutton android:id= "@+id/pause" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@+drawable/pause"/> < ImageButton AndroiD:id= "@+id/stop" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@+drawable/stop"/> </LinearLayout> <seekbar android:id= "@+id/seekbar" Andro Id:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/></linearlayout>
The. Java program is as follows:
Package Org.lxh.demo;import Java.io.ioexception;import Android.app.activity;import android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.media.mediaplayer;import Android.media.mediaplayer.oncompletionlistener;import Android.os.asynctask;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.imagebutton;import Android.widget.seekbar;import Android.widget.seekbar.onseekbarchangelistener;import Android.widget.TextView; public class Hello extends Activity {private ImageButton play = null;private ImageButton pause = null;private ImageButton Stop = null;private TextView info = null;private MediaPlayer Mymediaplayer = null;private Boolean pauseflag = False;privat e Boolean playflag = true;private SeekBar SeekBar = null;public void OnCreate (Bundle savedinstancestate) {super.oncreate (sAvedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use This.info = (TextView) Super.findviewbyid (r.id.info); this.play = (ImageButton) Super.findviewbyid ( R.id.play); this.pause = (ImageButton) Super.findviewbyid (r.id.pause); this.stop = (ImageButton) Super.findviewbyid ( R.id.stop); This.seekbar = (Seekbar) Super.findviewbyid (R.id.seekbar); This.play.setOnClickListener (new Playonclicklistener ()); This.pause.setOnClickListener (new Pauseonclicklistener ()); This.stop.setOnClickListener ( New Stoponclicklistener ()); This.seekbar.setOnSeekBarChangeListener (new Seekbarchangelistener ());} Private class Playonclicklistener implements Onclicklistener {public void OnClick (View arg0) {if ( Hello.this.myMediaPlayer = = null) {Hello.this.myMediaPlayer = Mediaplayer.create (HELLO.THIS,R.RAW.MLDN_AD);// Locate Resource Hello.this.myMediaPlayer.setOnCompletionListener (new Oncompletionlistener () {///file has finished playing public void Oncompletion ( MediaPlayer mymediaplayer) {Hello.this.playFlag = false; Hello.this.myMediaPlaYer.release ();}}); Hello.this.seekbar.setMax (Hello.this.myMediaPlayer.getDuration ()); Updateseekbar update = new Updateseekbar (); Update.execute (+); try {Hello.this.myMediaPlayer.prepare ();} catch ( IllegalStateException e) {Hello.this.info.setText ("File Play exception ..." + e);} catch (IOException e) {Hello.this.info.setText ("File Play exception ..." + e);} Hello.this.myMediaPlayer.start (); Hello.this.info.setText ("Playing audio file ...");}} Private class Updateseekbar extends Asynctask<integer, Integer, string> {@Overrideprotected void OnPostExecute ( String result) {} @Overrideprotected void Onprogressupdate (Integer ... progress) {Hello.this.seekbar.setProgress ( Progress[0]);//update drag bar} @Overrideprotected String doinbackground (Integer ... params) {while (Hello.this.playFlag) {try { Thread.Sleep (Params[0]);} catch (Interruptedexception e) {e.printstacktrace ();} This.publishprogress (Hello.this.myMediaPlayer.getCurrentPosition ());} return null;}} Private class Pauseonclicklistener implements Onclicklistener {public void OnClick (ViewARG0) {if (Hello.this.myMediaPlayer! = null) {if (Hello.this.pauseFlag) {Hello.this.myMediaPlayer.start (); Hello.this.pauseFlag = false;} else {Hello.this.myMediaPlayer.pause (); Hello.this.pauseFlag = True;}}} Private class Stoponclicklistener implements Onclicklistener {public void OnClick (View arg0) {if ( Hello.this.myMediaPlayer = null) {Hello.this.myMediaPlayer.stop (); Hello.this.info.setText ("Stop playing Audio");}}} Private class Seekbarchangelistener implements Onseekbarchangelistener {public void onprogresschanged (SeekBar arg0, int Arg1, Boolean arg2) {}public void Onstarttrackingtouch (SeekBar arg0) {}public void Onstoptrackingtouch (SeekBar arg0) {//drag Hello.this.myMediaPlayer.seekTo (Hello.this.seekbar.getProgress ()) After moving the progress bar;}}}
Run the following example:
Note: This example may have many bugs and is only used for MediaPlayer's basic usage display.
Android--mediaplayer Implement MP3 Playback applet