Android MediaController seekbar not refreshing

我怕爱的太早我们不能终老 提交于 2019-12-11 06:58:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!