|
| 1 | +package com.hrupin.streamingmedia; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.media.MediaPlayer; |
| 5 | +import android.media.MediaPlayer.OnBufferingUpdateListener; |
| 6 | +import android.media.MediaPlayer.OnCompletionListener; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.os.Handler; |
| 9 | +import android.view.MotionEvent; |
| 10 | +import android.view.View; |
| 11 | +import android.view.View.OnClickListener; |
| 12 | +import android.view.View.OnTouchListener; |
| 13 | +import android.widget.EditText; |
| 14 | +import android.widget.ImageButton; |
| 15 | +import android.widget.SeekBar; |
| 16 | + |
| 17 | +import com.hrupin.streamingmedia.R; |
| 18 | + |
| 19 | +public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ |
| 20 | + |
| 21 | + private ImageButton buttonPlayPause; |
| 22 | + private SeekBar seekBarProgress; |
| 23 | + public EditText editTextSongURL; |
| 24 | + |
| 25 | + private MediaPlayer mediaPlayer; |
| 26 | + private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class |
| 27 | + |
| 28 | + private final Handler handler = new Handler(); |
| 29 | + |
| 30 | + /** Called when the activity is first created. */ |
| 31 | + @Override |
| 32 | + public void onCreate(Bundle savedInstanceState) { |
| 33 | + super.onCreate(savedInstanceState); |
| 34 | + setContentView(R.layout.main); |
| 35 | + initView(); |
| 36 | + } |
| 37 | + |
| 38 | + /** This method initialise all the views in project*/ |
| 39 | + private void initView() { |
| 40 | + buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause); |
| 41 | + buttonPlayPause.setOnClickListener(this); |
| 42 | + |
| 43 | + seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay); |
| 44 | + seekBarProgress.setMax(99); // It means 100% .0-99 |
| 45 | + seekBarProgress.setOnTouchListener(this); |
| 46 | + editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL); |
| 47 | + editTextSongURL.setText(R.string.testsong_20_sec); |
| 48 | + |
| 49 | + mediaPlayer = new MediaPlayer(); |
| 50 | + mediaPlayer.setOnBufferingUpdateListener(this); |
| 51 | + mediaPlayer.setOnCompletionListener(this); |
| 52 | + } |
| 53 | + |
| 54 | + /** Method which updates the SeekBar primary progress by current song playing position*/ |
| 55 | + private void primarySeekBarProgressUpdater() { |
| 56 | + seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length" |
| 57 | + if (mediaPlayer.isPlaying()) { |
| 58 | + Runnable notification = new Runnable() { |
| 59 | + public void run() { |
| 60 | + primarySeekBarProgressUpdater(); |
| 61 | + } |
| 62 | + }; |
| 63 | + handler.postDelayed(notification,1000); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onClick(View v) { |
| 69 | + if(v.getId() == R.id.ButtonTestPlayPause){ |
| 70 | + /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */ |
| 71 | + try { |
| 72 | + mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source |
| 73 | + mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. |
| 74 | + } catch (Exception e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + |
| 78 | + mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL |
| 79 | + |
| 80 | + if(!mediaPlayer.isPlaying()){ |
| 81 | + mediaPlayer.start(); |
| 82 | + buttonPlayPause.setImageResource(R.drawable.button_pause); |
| 83 | + }else { |
| 84 | + mediaPlayer.pause(); |
| 85 | + buttonPlayPause.setImageResource(R.drawable.button_play); |
| 86 | + } |
| 87 | + |
| 88 | + primarySeekBarProgressUpdater(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean onTouch(View v, MotionEvent event) { |
| 94 | + if(v.getId() == R.id.SeekBarTestPlay){ |
| 95 | + /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/ |
| 96 | + if(mediaPlayer.isPlaying()){ |
| 97 | + SeekBar sb = (SeekBar)v; |
| 98 | + int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress(); |
| 99 | + mediaPlayer.seekTo(playPositionInMillisecconds); |
| 100 | + } |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onCompletion(MediaPlayer mp) { |
| 107 | + /** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/ |
| 108 | + buttonPlayPause.setImageResource(R.drawable.button_play); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onBufferingUpdate(MediaPlayer mp, int percent) { |
| 113 | + /** Method which updates the SeekBar secondary progress by current song loading from URL position*/ |
| 114 | + seekBarProgress.setSecondaryProgress(percent); |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments