Android Music Playback-mediaplayer

Source: Internet
Author: User

When you are bored by bus, when you drown in the subway crowd, do you want to keep a quiet in your heart, then please bring headphones, open your music player, listen to an old song to take you into the quiet world, music play this function before the advent of smartphones, the Nokia era, Even when it comes back to the start of the mobile phone, it belongs to the audio category. One of the unavoidable uses for playing audio in Android is MediaPlayer, which is also the same class as video calls. Pull away, get to the point:

Basic Maintenance

First of all, let's take a look at this time to achieve the effect:

The layout is as follows:

<linearlayout xmlns: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:orien tation= "vertical" tools:context= "com.example.googlemedia.MainActivity" > <edittext android:id= "@+id/edi T_musicpath "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:hint=" output Into the path you like the song "/> <linearlayout android:layout_width=" match_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <button android:id=" @+id/btn_play "Android:            Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "PlayEvent" android:text= "Play"/> <button android:id= "@+id/btn_pause" android:layout_width= "wrap _content "android:layout_height=" WraP_content "android:onclick=" pauseevent "android:text=" pause "/> <button Android            : id= "@+id/btn_stop" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:onclick= "Stopevent" android:text= "Stop"/> <button android:id= "@+id/btn_repla Y "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:oncli ck= "Replayevent" android:text= "Replay"/> </LinearLayout></LinearLayout>
Demo Complete

Audio files:

Play button Event:

     public void playevent (view view) {edittext= (EditText) Findviewbyid (R.id.edit_musicpath);      String Pathstring=edittext.gettext (). toString (). Trim ();      File File=new file (pathstring);  if (file.exists ()) {try {mediaPlayer = new MediaPlayer ();  Mediaplayer.setaudiostreamtype (Audiomanager.stream_music);  Mediaplayer.setdatasource (pathstring);  Mediaplayer.prepare ();  Mediaplayer.start ();  Multiple Click play button Easy Mix btn_playbutton.setenabled (false); Set Display Mediaplayer.setoncompletionlistener (new Oncompletionlistener () {@Overridepublic Void when callback is required after playback Oncompletion (MediaPlayer MP) {//TODO auto-generated Method Stub btn_playbutton.setenabled (true);}});} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (SecurityException e) { TODO auto-generated catch Blocke.printstacktrace ();} catch (IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated Catch Blocke. Printstacktrace ();}}     else {Toast.maketext (this, "Sorry, there is a problem with the path you entered, please check carefully", toast.length_short). Show (); }

Playback effect:

Pause and Resume Events:

         Public  void  pauseevent (view view) {     if (Btn_pausebutton.gettext (). Equals ("continue")) {Mediaplayer.start (); Btn_pausebutton.settext ("pause"); return;}     if (mediaplayer!=null&&mediaplayer.isplaying ()) {     mediaplayer.pause ();     Btn_pausebutton.settext ("Continue");}     }

Pause and Resume Effects:

Stop event:

    Public  void Stopevent (view view) {     if (mediaplayer!=null&&mediaplayer.isplaying ()) {     Btn_ Playbutton.setenabled (true);     Mediaplayer.stop ();     Release MediaPlayer Otherwise the memory     Mediaplayer.release () will be consumed;     Mediaplayer=null;}     Btn_pausebutton.settext ("pause");     Btn_playbutton.setenabled (True);     }

Replay Event:

     Public  void  replayevent (view view) {     if (mediaplayer!=null&&mediaplayer.isplaying ()) {     mediaplayer.seekto (0);} else {playevent (view);}     The playback status should be set when replaying     btn_playbutton.setenabled (TRUE);     }

A little simple write, if there is inappropriate, please give us a lot of advice ~

Android Music Playback-mediaplayer

Related Article

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.