Android MediaController Play / Pause and Seekbar button not updating

I am using MediaController.MediaPlayerControl to display the MediaController at the bottom of my custom view, but I cannot get it to work correctly. When I play music for the first time, then there should be a pause button, but there is playback instead, and when I press this button, the music pauses correctly and the state remains unchanged and after that it works correctly. Also, when I play the next song, the old MediaController widget overlaps the new one. And sometimes the progress / search indicator is not updated during music playback. It just updates when something is clicked on the MediaController (Play / Pause, forward, etc.).

I found these questions to be similar to mine, but I don’t think the answers they received solve my problem.

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 the controller:

public void playMusic()
{
        musicSrv.playSong(); //Play song in a service
        setController();
        controller.show(0);
        controller.requestFocus();
}
+4
source share
1 answer

I had exactly this problem. I don’t know if you need help, but I thought I would send it anyway. For posterity, do you follow this tutorial ?

-, : - setController(). :

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

, , , ( : Android , , ).

  • , , . :

    @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);
    }
    
  • , , . :

    // 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);
        }
    };
    
  • onResume():

    // Set up receiver for media player onPrepared broadcast
    LocalBroadcastManager.getInstance(this).registerReceiver(onPrepareReceiver,
            new IntentFilter("MEDIA_PLAYER_PREPARED"));
    
  • . setController(): onCreate() onResume() . controller.show(0) onResume().

+11

All Articles