Online music Playback Demo

Source: Internet
Author: User

Package Com.multimediademo7audiohttpplayer;import Android.app.activity;import Android.media.mediaplayer;import Android.media.mediaplayer.onbufferingupdatelistener;import Android.media.MediaPlayer.OnCompletionListener; Import Android.media.mediaplayer.onerrorlistener;import Android.media.mediaplayer.onpreparedlistener;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import android.widget.textview;/** * Online music playback demo. * */public class Audiohttpplayer extends Activity implements Oncompletionlistener,onerrorlistener, Onbufferingupdatelistener, Onpreparedlistener,onclicklistener {private MediaPlayer mediaplayer;private Button Startbutton, Stopbutton;private TextView Statustextview, buffervaluetextview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_audio_httpplayer); init ();} /** * Instantiate the component. */private void init () {Startbutton= (Button) Findviewbyid (R.id.startbutton), Stopbutton = (button) Findviewbyid (R.id.stopbutton); Statustextview = ( TextView) Findviewbyid (r.id.statusdisplaytextview); Buffervaluetextview = (TextView) Findviewbyid ( R.id.buffervaluetextview); Startbutton.setonclicklistener (this), Stopbutton.setonclicklistener (this); Statustextview.settext ("OnCreate"); mediaPlayer = new MediaPlayer (); Mediaplayer.setoncompletionlistener (this); Mediaplayer.setonerrorlistener (This), Mediaplayer.setonbufferingupdatelistener (this); Mediaplayer.setonpreparedlistener (this); Statustextview.settext ("MediaPlayer created"); try {/** * call Setdatasource () method, and pass in the HTTP location of the audio file you want to play. */mediaplayer.setdatasource ("Http://zhangmenshiting.baidu.com/data2/music/36865035/17765263198000128.mp3?xcode =81bb2569613f49564c53ca228d87e526d7ed2c9c576c829e ");} catch (Exception e) {e.printstacktrace ();} Statustextview.settext ("Setdatasource done"); Statustextview.settext ("Calling Prepareasync");/** * Call PrepareAsync method , it starts buffering the audio file in the background and returns. */mediaplayer.prepareAsync ();} /** * When MediaPlayer is buffering, the activity's Onbufferingupdate method is called. */@Overridepublic void Onbufferingupdate (MediaPlayer MP, int percent) {Buffervaluetextview.settext ("" + percent + "%");} /** * When the Prepareasync method is completed, the Onprepared method is called to indicate that the audio is ready to play. */@Overridepublic void onprepared (MediaPlayer MP) {Statustextview.settext ("onprepared called"); Startbutton.setenabled (TRUE);} /** * When MediaPlayer finishes playing the audio file, the Oncompletion method is called. * When the "Play" button is clicked, the "Pause" button does not look at the click (indicating that it can be played again). */@Overridepublic void Oncompletion (MediaPlayer MP) {Statustextview.settext ("oncompletion called"); Startbutton.setenabled (True); stopbutton.setenabled (false);} /** * When you press the return key on the phone, the system's OnKeyDown method is called automatically, and the OnKeyDown method calls the OnDestroy () method to destroy the activity *, if the OnDestroy () method does not rewrite, Then the music that is playing will not stop (you can try it), so this time to rewrite the OnDestroy () method, * in this method to add the Mediaplayer.stop () method, which means that when the return key is pressed, the MediaPlayer object's Stop method is called, To stop the music from playing. */@Overrideprotected void OnDestroy () {Super.ondestroy (); Mediaplayer.stop (); LOG.D ("Zhongyao", "OnDestroy ()");} /** * If MediaPlayer error occurs, the OnError method is called. */@Overridepublic Boolean OnError (MediaPlayer MP, int what, int extra) {Statustextview.settext ("onError called") and switch (what) {case MEDIAP Layer. MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:statusTextView.setText ("Media_error_not_valid_for_progressive_ PLAYBACK "+ extra); Break;case MediaPlayer.MEDIA_ERROR_SERVER_DIED:statusTextView.setText (" media_error_server_died "+ Extra"); Break;case MediaPlayer.MEDIA_ERROR_UNKNOWN:statusTextView.setText ("Media_error_unknown"); Break;default : Break;} return false;} /** * When the play button is pressed, the start method of MediaPlayer is called, and when the pause button is pressed, the MediaPlayer pause method is called. */@Overridepublic void OnClick (View v) {if (v = = Startbutton) {Mediaplayer.start (); Statustextview.settext ("Start called "); startbutton.setenabled (false); stopbutton.setenabled (true);} else if (v = = Stopbutton) {mediaplayer.pause (); Statustextview.settext ("pause called"); startbutton.setenabled (true); Stopbutton.setenabled (FALSE);}}}

Online music Playback Demo

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.