Android MediaPlayer Play music and implement progress bar

Source: Internet
Author: User

Work ahead and put music files in the SD card

1. layout file Main.xml

<?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"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Song name:"/> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Blueflawer.mp3"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Play"Android:id="@+id/play_pause"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text=" Reset"Android:id="@+id/reset"/> <SeekBar android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:id="@+id/seekbar"/></linearlayout>

2. Activity class

Package Cn.csdn.activity;import Java.io.file;import java.io.ioexception;import java.util.timer;import Java.util.timertask;import Android.app.activity;import Android.media.mediaplayer;import Android.os.Bundle;import Android.os.environment;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.seekbar;import Android.widget.SeekBar.OnSeekBarChangeListener; Public classMyplayeractivity extends Activity {PrivateButton play_pause, reset; PrivateSeekBar SeekBar; PrivateBoolean ifplay =false; PrivateMediaPlayer player =NULL; PrivateString Musicname ="Blueflawer.mp3"; PrivateBoolean Iffirst =false; PrivateTimer Mtimer; PrivateTimerTask Mtimertask; PrivateBoolean ischanging=false;//mutually exclusive variables to prevent the timer from Seekbar when dragging with the     Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Player=NewMediaPlayer (); Findviews ();//each component    }    Private voidfindviews () {Play_pause=(Button) Findviewbyid (r.id.play_pause); Reset=(Button) Findviewbyid (R.id.reset); Play_pause.setonclicklistener (NewMyclick ()); Reset.setonclicklistener (NewMyclick ()); Seekbar=(SeekBar) Findviewbyid (R.id.seekbar); Seekbar.setonseekbarchangelistener (NewMyseekbar ()); }    classMyclick implements Onclicklistener { Public voidOnClick (View v) {File file=NewFile (Environment.getexternalstoragedirectory (), musicname); //determine if there are any files to play            if(File.exists ()) {Switch(V.getid ()) { CaseR.id.play_pause:if(Player! =NULL&&!Ifplay) {Play_pause.settext ("Pause"); if(!Iffirst)                            {Player.reset (); Try{Player.setdatasource (File.getabsolutepath ()); Player.prepare ();//Prepare                            } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(IllegalStateException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Seekbar.setmax (Player.getduration ());//set the progress bar//----------Timer record playback Progress---------//Mtimer =NewTimer (); Mtimertask=NewTimerTask () {@Override Public voidrun () {if(ischanging==true) {                                         return;                                } seekbar.setprogress (Player.getcurrentposition ());                             }                              }; Mtimer.schedule (Mtimertask,0,Ten); Iffirst=true; } player.start ();//StartIfplay =true; } Else if(Ifplay) {Play_pause.settext ("continue to");                        Player.pause (); Ifplay=false; }                     Break;  CaseR.id.reset:if(Ifplay) {Player.seekto (0); } Else{player.reset (); Try{Player.setdatasource (File.getabsolutepath ()); Player.prepare ();//PreparePlayer.start ();//Start}Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(IllegalStateException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                    }                     Break; }            }        }    }    //Progress bar Processing    classMyseekbar implements Onseekbarchangelistener { Public voidOnprogresschanged (SeekBar SeekBar,intProgress, Boolean Fromuser) {        }         Public voidOnstarttrackingtouch (SeekBar SeekBar) {ischanging=true; }         Public voidOnstoptrackingtouch (SeekBar SeekBar) {Player.seekto (seekbar.getprogress ()); Ischanging=false; }    }    //Call Processing    protected voidOnDestroy () {if(Player! =NULL){            if(Player.isplaying ()) {player.stop ();        } player.release ();    } Super.ondestroy (); }    protected voidOnPause () {if(Player! =NULL){            if(Player.isplaying ()) {player.pause ();    }} super.onpause (); }    protected voidOnresume () {if(Player! =NULL){            if(!player.isplaying ())            {Player.start ();    }} super.onresume (); }}

Android MediaPlayer Play music and implement progress bar

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.