问题
I am playing mp3 file from url, but SeekBar is not updating while playing song.
When i am trying to move forcefully then song playing from start.
Below code i am using to play and update SeekBar.
I wanted to create a seekBar that track the progress of a mediaplayer but it doesnt work out quite well, the music is playing but the seekbar stay idle. Is there something that I left out?
I think i am not not calling updateSeekBar() method from correct position...so Tried by change position of updateSeekBar() but not worked...
Am I missing anything...? Please help.... I am new to Android...
Activity.java
Play = findViewById(R.id.Play_button);
Play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
play (url);//Song Url
}
});
private void play(String url) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
try {
if (temp == playurl.length) {
temp = 0;
play(playurl[temp]);
t1.setText(mobileArray[temp]);
} else {
temp = temp + 1;
play(playurl[temp]);
t1.setText(mobileArray[temp]);
}
}catch(ArrayIndexOutOfBoundsException exception) {
temp = 0;
play(playurl[temp]);
t1.setText(mobileArray[temp]);
}
}
});
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception ex){
String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
Log.e("sdcard-err2:",err);
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
}catch (Exception ex){
String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
Log.e("sdcard-err2:",err);
}
try {
mediaPlayer.start();
}catch (Exception ex){
String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
Log.e("sdcard-err2:",err);
}
finalTime = mediaPlayer.getDuration();
updateSeekBar();
startTime = mediaPlayer.getCurrentPosition();
}
private void updateSeekBar() {
seekBar.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLength) * 100));
if (mediaPlayer.isPlaying()) {
Runnable updater = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};seekBar.post(updater);
}
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
seekBar.setSecondaryProgress(percent);
}
回答1:
try this
mediaFileLength = mediaPlayer.getDuration();
realtimeLength = mediaFileLength;
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
updateSeekBar();
回答2:
Try
SeekBar.setProgress(0);
SeekBar.setMax(100);
after the mediaPlayer.start();
来源:https://stackoverflow.com/questions/54692981/android-music-seekbar-is-not-working-idle