package com.hrupin.streamingmedia;
002.
003.import android.app.Activity;
004.import android.media.MediaPlayer;
005.import android.media.MediaPlayer.OnBufferingUpdateListener;
006.import android.media.MediaPlayer.OnCompletionListener;
007.import android.os.Bundle;
008.import android.os.Handler;
009.import android.view.MotionEvent;
010.import android.view.View;
011.import android.view.View.OnClickListener;
012.import android.view.View.OnTouchListener;
013.import android.widget.EditText;
014.import android.widget.ImageButton;
015.import android.widget.SeekBar;
016.
017.import com.hrupin.media.R;
018.
019.public class StreamingMp3Player extends Activity implementsOnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
020.
021.private ImageButton buttonPlayPause;
022.private SeekBar seekBarProgress;
023.public EditText editTextSongURL;
024.
025.private MediaPlayer mediaPlayer;
026.private intmediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
027.
028.private final Handler handler = new Handler();
029.
030./** Called when the activity is first created. */
031.@Override
032.public void onCreate(Bundle savedInstanceState) {
033.super.onCreate(savedInstanceState);
034.setContentView(R.layout.main);
035.initView();
036.}
037.
038./** This method initialise all the views in project*/
039.private void initView() {
040.buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
041.buttonPlayPause.setOnClickListener(this);
042.
043.seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
044.seekBarProgress.setMax(99); // It means 100% .0-99
045.seekBarProgress.setOnTouchListener(this);
046.editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
047.editTextSongURL.setText(R.string.testsong_20_sec);
048.
049.mediaPlayer = new MediaPlayer();
050.mediaPlayer.setOnBufferingUpdateListener(this);
051.mediaPlayer.setOnCompletionListener(this);
052.}
053.
054./** Method which updates the SeekBar primary progress by current song playing position*/
055.private void primarySeekBarProgressUpdater() {
056.seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
057.if (mediaPlayer.isPlaying()) {
058.Runnable notification = new Runnable() {
059.public void run() {
060.primarySeekBarProgressUpdater();
061.}
062.};
063.handler.postDelayed(notification,1000);
064.}
065.}
066.
067.@Override
068.public void onClick(View v) {
069.if(v.getId() == R.id.ButtonTestPlayPause){
070./** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
071.try {
072.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
073.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.
074.} catch (Exception e) {
075.e.printStackTrace();
076.}
077.
078.mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL
079.
080.if(!mediaPlayer.isPlaying()){
081.mediaPlayer.start();
082.buttonPlayPause.setImageResource(R.drawable.button_pause);
083.}else {
084.mediaPlayer.pause();
085.buttonPlayPause.setImageResource(R.drawable.button_play);
086.}
087.
088.primarySeekBarProgressUpdater();
089.}
090.}
091.
092.@Override
093.public boolean onTouch(View v, MotionEvent event) {
094.if(v.getId() == R.id.SeekBarTestPlay){
095./** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
096.if(mediaPlayer.isPlaying()){
097.SeekBar sb = (SeekBar)v;
098.intplayPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
099.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.}