问题
I'm using a MediaController
and MediaPlayer
together, to create a simple audio player in Android, taken from an example I found here on this site.
However, after much search I could not find the solution to my problem: the progress/seek bar doesn't refresh while the music is playing. It just updates itself when something on the MediaController
is pressed (Play/Pause, forward, etc).
Any easy fix for this that I'm not getting ?
NOTE: My MediaController
is declared in XML
, and my MediaController.MediaPlayerControl
methods just make use of the MediaPlayer
class.
回答1:
Mediaplayer
provides a method getCurrentPosition()
you can put this in a thread to continously update the progressbar.
public int getCurrentPositionInt(){
if (player != null)
return player.getCurrentPosition();
else
return 0;
}
Create a Thread or CountDownTimer to continuously update the seekbar :
seekBar.setMax((getCurrentPositionInt() / 1000));
OR
MediaController.MediaPlayerControl mp;
mp.seekTo((getCurrentPositionInt() / 1000))
回答2:
Im Sorry for my English!
you are showing the controller before the music player is ready. You need to notify your activity from the controller when it is ready.
public void onPrepared(MediaPlayer mp) {
mp.start();
Intent onPreparedIntent = new Intent("MP_READY");
LocalBroadcastManager.getInstance(activity).sendBroadcast(onPreparedIntent);
}
Then you need to create a BroadcastReceiver in your activity and override his onReceive method to show the controller.
private BroadcastReceiver mpReadyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
controller.show(0);
}
};
You also need to register the receiver in your activity`s onResume().
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mpReadyReceiver,
new IntentFilter("MP_READY"));
}
Now try to call controller.show only when it is necesary.
Be careful not creating more than one controller instance
来源:https://stackoverflow.com/questions/12011631/android-mediacontroller-seekbar-not-refreshing