The MediaPlayer class for music media playback in Android is defined as follows
Final MediaPlayer Mprain = mediaplayer.create (this, r.raw.rain);
The file is Res/raw/rain.mp3//raw I created it myself in Androidstudio.
My own usage here is that there is only one button, click Use, if it stops on playback, otherwise start playing, attach code
1Mprain.setlooping (true);2Buttonrain.setonclicklistener (NewButton.onclicklistener () {3 @Override4 Public voidOnClick (View v) { A if(Israin = =false) { - Try { - if(Mprain! =NULL) { the mprain.stop (); - } - Mprain.prepare (); + Mprain.start (); -Israin =true; +Snackbar.make (V, "Rain start", Snackbar.length_long) A. Setaction ("Action",NULL). Show (); at}Catch(Exception e) { -Snackbar.make (V, "Rain Exception 1", Snackbar.length_long) -. Setaction ("Action",NULL). Show (); - } -}Else { - Try { in if(Mprain! =NULL) { to mprain.stop (); theSnackbar.make (V, "Rain stop", Snackbar.length_long) *. Setaction ("Action",NULL). Show (); $ }Panax Notoginseng}Catch(Exception e) { -Snackbar.make (V, "Rain Exception 2", Snackbar.length_long) the. Setaction ("Action",NULL). Show (); + } AIsrain =false; the } + } -});
where Mprain.start () is similar to pause (), release () and so on, it is recommended to take a look at the MediaPlayer documentation, there is a detailed explanation, there are also music playback to the loop
Mprain.setlooping (true);
There is also a need to rewrite the method I do not need here, not to rewrite
1 /*when Mediaplayer.oncompletionlister will run the listener2 Mp.setoncompletionlistener (3 new Mediaplayer.oncompletionlistener ()4 {5 //@Override6 / * Overwrite file broadcast Complete event7 Public void Oncompletion (MediaPlayer arg0)8 {9 TryTen { One /* Remove the assignment relationship between resources and MediaPlayer A * Allow resources to be used by other programs - mp.release (); - / * Change TextView to play end the Tv.settext ("Music advertisement end!"); - } - catch (Exception e) - { + Tv.settext (e.tostring ()); - e.printstacktrace (); + } A } at }); - - / * When Mediaplayer.onerrorlistener will run the listener - Mp.setonerrorlistener (New Mediaplayer.onerrorlistener () - { - @Override in / * Overwrite error handling events - Public Boolean onError (MediaPlayer arg0, int arg1, int arg2) to { + //TODO auto-generated method stub - Try the { * /* The assignment of resources and MediaPlayer is also lifted when an error occurs $ mp.release ();Panax Notoginseng Tv.settext ("Playback has occurred abnormally!"); - } the catch (Exception e) + { A Tv.settext (e.tostring ()); the e.printstacktrace (); + } - return false; $ } $ }); -
Control as much as possible to use a seekbar, according to the value of the get to set the MP volume, the method used to Mprain.setvolume (float left,float right)
1Seekbarrain.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener ()//Tuning Listener2 {3 Public voidOnprogresschanged (SeekBar arg0,intProgressBooleanFromuser) {4 //5 //Buttonrain.settext (Seekbarrain.getprogress ());6 7 //int tempvol = seekbarrain.getprogress ();8 //float tempvolf = tempvol/10.0f;9Mprain.setvolume (Seekbarrain.getprogress ()/10.0f, seekbarrain.getprogress ()/10.0f);Ten //log.i ("seek", "seekbarrain.getprogress ()" + Tempvolf); One //Mprain.setvolume (Seekbarrain.getprogress ()/10,seekbarrain.getprogress ()/10); A - } - the @Override - Public voidOnstarttrackingtouch (SeekBar SeekBar) { - //TODO auto-generated Method Stub - + } - + @Override A Public voidOnstoptrackingtouch (SeekBar SeekBar) { at //TODO auto-generated Method Stub - - - } -});
The two parameters of the SetVolume method are still not found in the usage and the difference, now the experiment results are set to the same value can directly set the volume value
android| Media | Playback and volume adjustment |seekbar|mediaplayer