Today, the study of the slag to play music with MediaPlayer to add a progress bar, the progress bar is now using the Android comes with the Seekbar, later will be followed by the UI, on the basis of the previous ability to play music, now add the main features are two:
1 show playback progress in real time
2 Manually adjust the playback progress
Now learn to build a new project to test, later and the previous blog introduction of the sliding page to integrate, it is estimated that the service is built after integration, please look forward to ....
All right, first up:
The basic principle of using a progress bar is simple, with a few parameters :
1 The length of the song: The unit is milliseconds, MediaPlayer getduration (), but pay attention to the use of getduration () state, in this blog introduced the MediaPlayer of the various states, you can go to see.
2 The length of the progress bar: the length of the progress bar in Android should default to 100, the unit is unknown, not affected
3 Current progress: For a song, the current progress is the location of the song currently playing, in milliseconds, for the progress bar, is the proportion of the progress bar. The main is to convert these two.
Two functions:
1 because progress needs to be updated in real time, a thread is sent to the UI thread in the system (every 100 milliseconds), the message is captured in the UI thread with handler, and the current position is obtained through the getcurrentposition of MediaPlayer. The corresponding position of Seekbar is calculated, and the Seekbar is updated in time.
2 for manually adjusting the progress of the song, by getting the adjustment to the position, through the Seekbar.onseekbarchangelistener () listening slider, when the slider adjustment position is determined, through Onstoptrackingtouch () to update the progress of the song in a timely way, Adjusts the progress of the song with MediaPlayer Seekto (in milliseconds).
The specific code is as follows:
public class Mainactivity extends Activity {MediaPlayer MediaPlayer; SeekBar seekbar;//The path to the local song string path = "/storage/sdcard1/music/Romantic Full House. mp3";//Process progress bar update Handler Mhandler = new Handler () {@ Override public void Handlemessage (Message msg) {switch (msg.what) {case 0://update progress int position = MEDIAPLAYER.GETCU Rrentposition (); int time = Mediaplayer.getduration (); int max = Seekbar.getmax (); Seekbar.setprogress (Position*max/time); Break Default:break;} }}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); init ();//Initialize the play button PlayButton = (button) Findviewbyid (R.id.playbutton); Playbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubtoast.maketext (Mainactivity.this, "Time Is" +mediaplayer.getduration (), +). Show ();p lay ();});//Initialize pause button Pausebutton = (button) Findviewbyid (R.id.pausebutton);p Ausebutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubpause ();}); SeekBar = (SeekBar) Findviewbyid (R.id.seekbar); Seekbar.setonseekbarchangelistener (new Seekbar.onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch (SeekBar SeekBar) {//Manually adjust progress//TODO Auto-generated method Stubint dest = seekbar.getprogress (); int time = Mediaplayer.getduration (); int max = Seekbar.getmax ( ); Mediaplayer.seekto (Time*dest/max);} @Overridepublic void Onstarttrackingtouch (SeekBar arg0) {//TODO auto-generated method stub} @Overridepublic void Onprogresschanged (SeekBar arg0, int arg1, Boolean arg2) {//TODO auto-generated Method stub}}); Initialize music to play void Init () {//Enter Idlemediaplayer = new MediaPlayer (); try {//Initialize Mediaplayer.setdatasource (path); Mediaplayer.setaudiostreamtype (Audiomanager.stream_music); Prepare load the media resources asynchronously Mediaplayer.prepareasync ();//BackgroundThread sends a message to update progress bar final int milliseconds = 100;new thread () {@Overridepublic void run () {while (true) {try { Sleep (milliseconds); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printsta Cktrace (); } mhandler.sendemptymessage (0); }}}.start ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Test play Music void Play () {Mediaplayer.start ();} Pause Music private void pause () {if (MediaPlayer! = null && mediaplayer.isplaying ()) {Me Diaplayer.pause (); }}//activity stop playing music when exiting, freeing resources @overrideprotected void OnDestroy () {//Recycle resources at end of activity if (me Diaplayer! = null && mediaplayer.isplaying ()) {mediaplayer.stop (); Mediaplayer.release (); MediaPlayer = null; } Super.ondestroY (); } @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is P Resent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}