Hello Guys,
Today I share Audio Streaming functionality with you.
Streaming is work like smoothly display your content.
For Ex. When you play any mp3 song or ringtone then its buffering and playing parallely so it will continuously playing without stop after few buffering.
User can enjoy their live music listening without any break.
Here I provide you sample code for Audio Streaming.
Permission: <uses-permission android:name="android.permission.INTERNET"/>
NOTE: YOU MUST NEED TO TEST THIS DEMO APP INTO YOUR DEVICE ONLY.
Sample Code:
File: StreamingMp3Player.java
/** This method initialise all the views in project*/
private void initView()
{
buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(this);
seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
editTextSongURL.setText(R.string.testsong_20_sec);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
/** Method which updates the SeekBar primary progress by current song playing position*/
private void primarySeekBarProgressUpdater()
{
seekBarProgress.setProgress((int)(((float)mediaPlayer. getCurrentPosition() / mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
if (mediaPlayer.isPlaying())
{
Runnable notification = new Runnable()
{
public void run()
{
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}
}
@Override
public void onClick(View v)
{
if(v.getId() == R.id.ButtonTestPlayPause)
{
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
try
{
// setup song from http://www.hrupin.com/wp-content/uploads/ mp3/ testsong_20_sec.mp3
// URL to mediaplayer data source
mediaPlayer.setDataSource(editTextSongURL.getText().toString());
// 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.
mediaPlayer.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
// gets the song length in milliseconds from URL
mediaFileLengthInMilliseconds = mediaPlayer.getDuration();
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
buttonPlayPause.setImageResource(R.drawable.button_pause);
}
else
{
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
}
Output:
Download Full Source Code From Here: AudioStreaming
Happy Coding...!!!
Today I share Audio Streaming functionality with you.
Streaming is work like smoothly display your content.
For Ex. When you play any mp3 song or ringtone then its buffering and playing parallely so it will continuously playing without stop after few buffering.
User can enjoy their live music listening without any break.
Here I provide you sample code for Audio Streaming.
Permission: <uses-permission android:name="android.permission.INTERNET"/>
NOTE: YOU MUST NEED TO TEST THIS DEMO APP INTO YOUR DEVICE ONLY.
Sample Code:
File: StreamingMp3Player.java
/** This method initialise all the views in project*/
private void initView()
{
buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(this);
seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
editTextSongURL.setText(R.string.testsong_20_sec);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
/** Method which updates the SeekBar primary progress by current song playing position*/
private void primarySeekBarProgressUpdater()
{
seekBarProgress.setProgress((int)(((float)mediaPlayer. getCurrentPosition() / mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
if (mediaPlayer.isPlaying())
{
Runnable notification = new Runnable()
{
public void run()
{
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}
}
@Override
public void onClick(View v)
{
if(v.getId() == R.id.ButtonTestPlayPause)
{
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
try
{
// setup song from http://www.hrupin.com/wp-content/uploads/ mp3/ testsong_20_sec.mp3
// URL to mediaplayer data source
mediaPlayer.setDataSource(editTextSongURL.getText().toString());
// 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.
mediaPlayer.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
// gets the song length in milliseconds from URL
mediaFileLengthInMilliseconds = mediaPlayer.getDuration();
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
buttonPlayPause.setImageResource(R.drawable.button_pause);
}
else
{
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
}
Output:
Download Full Source Code From Here: AudioStreaming
Happy Coding...!!!
No comments:
Post a Comment