Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaController Play/Pause button and Seekbar doesn't Refresh

I am using MediaController.MediaPlayerControl in order to display a MediaController at the bottom of my Custom View but I can't get it to work properly. When I play music for first time then there should be pause button visible but instead there is play and when I press that button then the music is paused correctly and state remains the same and after that its working properly. Also when I play next song, the old MediaController widget gets overlapped with the new one. And sometimes 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).

I found these questions similar to mine but I don't think the answers they got will solve my problem.

  1. Android mediacontroller Play Pause controls not refresh properly
  2. Android MediaController seekbar not refreshing
  3. Android VideoView Playback Controls Show "Play" Initially Instead of "Pause" Even Though File is Already Playing

This is how I initialize the controller:

private void setController()
{
    controller = new MusicController(this);

    controller.setPrevNextListeners(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            playNext();
          }
        }, new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            playPrev();
          }
        });

    controller.setMediaPlayer(this);
    controller.setAnchorView(findViewById(R.id.song_list));
    controller.setEnabled(true);
}

This is how I show controller:

public void playMusic()
{
        musicSrv.playSong(); //Play song in a service
        setController();
        controller.show(0);
        controller.requestFocus();
}
like image 598
user221458 Avatar asked Dec 15 '22 20:12

user221458


1 Answers

I had exactly this problem. Don't know if you still need help, but I thought I'd post anyway. For posterity, are you following this tutorial?

First, the easy problem: you're getting multiple instances of your controls because of repeated calls to setController(). Change the first line of your function to:

if (controller == null) controller = new MusicController(this);

With regards to the play button malfunctioning, I believe it's because you're showing it before the music player has been prepared (disclaimer: I'm a newbie to Android myself, and the following are the things I've found to have worked).

  1. Set up a broadcast from your music-playing service to notify your music-controlling activity when the musicplayer has been prepared. Append the following function in your music-playing service to broadcast intent:

    @Override
    public void onPrepared(MusicPlayer player) {
        // Do some other stuff...
    
        // Broadcast intent to activity to let it know the media player has been prepared
        Intent onPreparedIntent = new Intent("MEDIA_PLAYER_PREPARED");
        LocalBroadcastManager.getInstance(this).sendBroadcast(onPreparedIntent);
    }
    
  2. Set up a broadcast receiver in your music-controlling activity to receive the intent broadcast by your service. Add the following class to your activity:

    // Broadcast receiver to determine when music player has been prepared
    private BroadcastReceiver onPrepareReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent i) {
        // When music player has been prepared, show controller
        controller.show(0);
        }
    };
    
  3. Register your receiver in your activity's onResume() method:

    // Set up receiver for media player onPrepared broadcast
    LocalBroadcastManager.getInstance(this).registerReceiver(onPrepareReceiver,
            new IntentFilter("MEDIA_PLAYER_PREPARED"));
    
  4. Depending on the structure of your code, you'll have to do some general tidying up. In my code, I've only called setController() twice: from the onCreate() and onResume() methods in my activity. I also only call controller.show(0) from my broadcast receiver, and my onResume() method.

like image 69
Alec Avatar answered Dec 28 '22 23:12

Alec