1
votes

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.

2
Is the seekbar supposed to refresh automatically (through the MediaController/MediaPlayer APIs) or are you supposed to update it using code?Vishwa Patel
Well I wanted it to refresh automatically, since : mediaController.setMediaPlayer(mediaPlayer) is done. Unfortunately it only updates (without my code) when any of the MediaController's buttons are pressed.PedroN

2 Answers

1
votes

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)) 
0
votes

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