Actually I don't know where is error in your code but I will explain how I did that in my app
public void playMp3(String _link){
mediaPlayer.reset();
Progressbar.setVisibility(View.VISIBLE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(_link);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
//mediaPlayer.prepare(); // might take long! (for buffering, etc) //@@
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block///
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Then implements OnCompletionListener, OnPreparedListener and OnBufferingUpdateListener in your class
public class PlayerActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnBufferingUpdateListener{
.
.
.
and implements all methodes
public void onPrepared(MediaPlayer mediaplayer) {
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
Progressbar.setVisibility(View.INVISIBLE);
play.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
}
updateProgressBar();
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
//mediaPlayer.reset();
songProgressBar.setProgress(0);
songProgressBar.setSecondaryProgress(0);
play.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
link = "http://server11.mp3quran.net/hawashi/002.mp3";
playMp3(link);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
songProgressBar.setSecondaryProgress(percent);
if(percent==100)
{
Progressbar.setVisibility(View.INVISIBLE);
}else if(percent > songProgressBar.getProgress())
{
Progressbar.setVisibility(View.INVISIBLE);
}else
{
Progressbar.setVisibility(View.VISIBLE);
}
}
I wish this helped you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…