Android MediaPlayer online music player

Source: Internet
Author: User

This article describes an online music player. The operations are described in detail to achieve the effect and understand the principles of the player.

Source code:

Layout file:

Activity_audio_httpplayer:

     
      
      
      
      
  
 

AudioHTTPPLayer. java:

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 Droid. view. View. OnClickListener; import android. widget. Button; import android. widget. TextView;/*** online music Demo. **/Public class AudioHTTPPLayer extends Activity implements OnCompletionListener, OnErrorListener, listener, listener, OnClickListener {private MediaPlayer mediaPlayer; private Button startButton, stopButton; private TextView statusTextView, bufferValueTextView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. l Ayout. 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 Medi APlayer (); mediaPlayer. setOnCompletionListener (this); mediaPlayer. setOnErrorListener (this); mediaPlayer. setOnBufferingUpdateListener (this); mediaPlayer. setOnPreparedListener (this); statusTextView. setText ("MediaPlayer created"); try {/*** call setDataSource (); and pass in the HTTP position of the audio file to be played. */MediaPlayer. setDataSource ("http://zhangmenshiting.baidu.com/data2/music/36865035/17765263198000128.mp3? Xcode = 81bb2569613f49564c53ca228d87e526d7ed2c94106c829e ");} catch (Exception e) {e. printStackTrace ();} statusTextView. setText ("setDataSource done"); statusTextView. setText ("calling prepareAsync");/*** call the prepareAsync method, which will buffer the audio file and return it in the background. */MediaPlayer. prepareAsync ();}/*** when the MediaPlayer is buffering, The onBufferingUpdate method of the Activity is called. * // @ Overridepublic void onBufferingUpdate (MediaPlayer mp, int percent) {bufferValueTextView. setText ("" + percent + "%");}/*** when the prepareAsync method is completed, the onPrepared method is called, indicating that the audio is ready to be played. * // @ Overridepublic void onPrepared (MediaPlayer mp) {statusTextView. setText ("onPrepared called"); startButton. setEnabled (true);}/*** the onCompletion method is called when the MediaPlayer completes playing the audio file. * You can click the "play" button. If you click the "pause" button, you can play the video again ). * // @ Overridepublic void onCompletion (MediaPlayer mp) {statusTextView. setText ("onCompletion called"); startButton. setEnabled (true); stopButton. setEnabled (false);}/*** when you press the return key on your phone, the system's onKeyDown method is automatically called, and the onKeyDown method calls onDestroy () method To destroy the Activity *. If the onDestroy () method is not overwritten, the playing music will not stop (you can try it). Therefore, the onDestroy () method should be rewritten at this time, * Add mediaPlayer to this method. the stop () method calls the stop method of the mediaPlayer object when you press the return key to stop playing the music. * // @ Overrideprotected void onDestroy () {super. onDestroy (); mediaPlayer. stop (); Log. d ("zhongyao", "onDestroy ()");}/*** if a MediaPlayer error occurs, the onError method is called. * // @ Overridepublic boolean onError (MediaPlayer mp, int what, int extra) {statusTextView. setText ("onError called"); switch (what) {case MediaPlayer. 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;}/*** call the MediaPlayer start method when the play button is pressed. When the pause button is pressed, the Pause method of MediaPlayer 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 );}}}

Source code download:

Click to download source code

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.