Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaController play button not updating when song is playing

I am trying to use the mediacontroller to play audio. I am able to get the audio working but when the song starts playing the play button on my mediacontroller does not update. If press the play button on the mediacontroller it pauses the song and seeks the progressbar. Then if i press pause it plays the song again. i am calling

mp.start() 

does this not update the play button. How can I get the button to change to the pause button when I call start?

public class QuickMusicPlayer implements MediaController.MediaPlayerControl {

private final MediaPlayer mMediaPlayer;
private final MediaController mMediaController;
private Activity mActivity;

public QuickMusicPlayer(View anchorView, Activity activity) {
    mActivity = activity;
    mMediaPlayer = new MediaPlayer();
    mMediaController = new MediaController(activity);
    mMediaController.setMediaPlayer(this);
    mMediaController.setAnchorView(anchorView);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}

public void setUrl(String url) {
    mMediaPlayer.reset();
    if (TextUtils.isEmpty(url)) {
        ViewUtil.showCroutonAlert(mActivity, R.string.cant_play_music);
    } else {
        try {

            mMediaPlayer.setDataSource(url);
            mMediaPlayer.prepareAsync();
            mMediaController.setEnabled(true);
            mMediaController.show(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void start() {
    mMediaPlayer.start();
}

@Override
public void pause() {
    mMediaPlayer.pause();
}

@Override
public int getDuration() {
    return mMediaPlayer.getDuration();
}

@Override
public int getCurrentPosition() {
    return mMediaPlayer.getCurrentPosition();
}

@Override
public void seekTo(int pos) {
    mMediaPlayer.seekTo(pos);
}

@Override
public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}

@Override
public int getBufferPercentage() {
    return 0;
}

@Override
public boolean canPause() {
    return true;
}

@Override
public boolean canSeekBackward() {
    return true;
}

@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getAudioSessionId() {
    return 0;
}

}

like image 404
user1634451 Avatar asked Sep 05 '14 18:09

user1634451


1 Answers

Figured it out. I need to call show again after start to update the buttons

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        mMediaController.show(0);
    }
});
like image 142
user1634451 Avatar answered Sep 19 '22 14:09

user1634451