android--Simple music player

Source: Internet
Author: User

Use MediaPlayer to do simple music player, more content please go to Baidu experience view http://jingyan.baidu.com/article/60ccbceb63452364cab197f1.html

<relativelayout 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:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Android:paddingbottom="@dimen/activity_vertical_margin"tools:context=". Mainactivity"Android:id="@+id/relativelayout"> <textview android:text="Audio Player"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:id="@+id/textview"/> <LinearLayout android:orientation="Horizontal"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:layout_below="@+id/textview"Android:layout_alignparentleft="true"Android:layout_alignparentstart="true"> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Play"Android:id="@+id/button"Android:layout_weight="0.33"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Stop"Android:id="@+id/button2"Android:layout_weight="0.33"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Pause"Android:id="@+id/button3"Android:layout_weight="0.33"/> </LinearLayout></RelativeLayout>
Package Com.basillee.asus.demo;import Android.app.activity;import Android.media.mediaplayer;import Android.os.bundle;import Android.os.environment;import Android.util.log;import Android.view.View;import Android.widget.button;import Android.widget.textview;import Android.widget.toast;import Java.io.File;public class     Mainactivity extends Activity {private MediaPlayer Mediaplayer;//mediaplayer object Private Boolean ispause=false;//paused Private file file;//files to be played private TextView hint;//the text box that declares the hint information @Override protected void onCreate (Bundle savedi        Nstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Get each Function button Final button button= (button) Findviewbyid (R.id.button);//Play Final button button1= (button) Findviewby Id (R.id.button2);//Pause Final button button2= (button) Findviewbyid (r.id.button3);//Stop hint= (TextView) Findviewby        Id (R.id.textview); if (!isfileexist ()) {button.setenabled (false);        }//Add event listener to MediaPlayer object and restart music playback when playback is complete mediaplayer.setoncompletionlistener (new mediaplayer.oncomple            Tionlistener () {@Override public void Oncompletion (MediaPlayer MP) {play ();        }        }); Event Listener for Play button Button.setonclicklistener (new View.onclicklistener () {@Override public void on                Click (View v) {play ();                    if (ispause) {button1.settext ("pause");                Ispause=false;                } button1.setenabled (True);                Button2.setenabled (TRUE);            Button.setenabled (FALSE);        }        }); Add Event Listener Button1.setonclicklistener (new View.onclicklistener () {@Override public Vo to pause, continue button                     ID OnClick (View v) {if (mediaplayer.isplaying () &&!ispause) {mediaplayer.pause ();                   Ispause=true; (button) v). SetText ("continue");                    Hint.settext ("Pause audio playback ....");                Button.setenabled (TRUE);                    }else{Mediaplayer.start ();                    (button) v). SetText ("pause");                    Hint.settext ("Continue playing audio ....");                Button.setenabled (FALSE);        }            }        }); Add an event listener to the Stop button Button2.setonclicklistener (new View.onclicklistener () {@Override public void                OnClick (View v) {mediaplayer.stop ();                Hint.settext ("Stop playing audio ...");                Button1.setenabled (FALSE);                Button2.setenabled (FALSE);            Button.setenabled (TRUE);    }        });        } @Override protected void OnDestroy () {if (mediaplayer.isplaying ()) {mediaplayer.stop ();        } mediaplayer.release ();    Super.ondestroy (); }//Determine if the file exists private Boolean isfileexist () {file=new Files (environment. getExternalStorageDirectory () +file.separator+ "Mymp32.mp3");            if (file.exists ()) {//Mediaplayer=new mediaPlayer ();            Mediaplayer=mediaplayer.create (this,r.raw.my);                 try {//Mediaplayer.setdatasource (File.getabsolutepath ());//Mediaplayer.prepare ();//Pre-load audio//            Mediaplayer.start ();//Play Music}catch (Exception e) {e.printstacktrace ();            } toast.maketext (this, "file exist", Toast.length_long). Show ();        return true;        }else{Toast.maketext (This, "file don t exist", Toast.length_long). Show ();    } return false; }//The method of playing music private void play () {try{mediaplayer.reset ();//new settings to play Music//mediaplayer.se Tdatasource (File.getabsolutepath ());//Mediaplayer.prepare ();//Pre-load audio mediaplayer=mediaplayer.create (            this,r.raw.my); Mediaplayer.start ();//Play Music Hint.settext ("Music is STarting ");            } catch (Exception e) {e.printstacktrace ();        LOG.E ("Err", E.getmessage ());    } return; }}

Use MediaPlayer to do simple music player, more content please go to Baidu experience view http://jingyan.baidu.com/article/60ccbceb63452364cab197f1.html

android--Simple music player

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.