简体   繁体   中英

MediaController Play Icon to change to Pause Icon programmatically

I am using exoPlayer to play music, I am controlling the music through mediaController that android provides. What I really want is that any external events such as phone call should not only pause playback but also change the icon to pause. I have managed to use the telephoneManager Api to find whether I am on call or not and I am able to pause the mediaPlayBack on Pause but I am clearly unable to change the playback icon to pause on this call event.

The MediaPlayerControl has a pause() function which I call where I pause the playback. The playBack pauses but icon doesn't change, Let me know if there is any good way to do so

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {

            PlayerControl.pause();

        }

        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            PlayerControl.pause();

        }

        super.onCallStateChanged(state, incomingNumber);
    }
};


public class PlayerControl implements MediaPlayerControl {

  private final ExoPlayer exoPlayer;

  public PlayerControl(ExoPlayer exoPlayer) {
    this.exoPlayer = exoPlayer;
  }

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

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

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

  /**
   * This is an unsupported operation.
   * <p>
   * Application of audio effects is dependent on the audio renderer used. When using
   * {@link com.google.android.exoplayer.MediaCodecAudioTrackRenderer}, the recommended approach is
   * to extend the class and override
   * {@link com.google.android.exoplayer.MediaCodecAudioTrackRenderer#onAudioSessionId}.
   *
   * @throws UnsupportedOperationException Always thrown.
   */
  @Override
  public int getAudioSessionId() {
    throw new UnsupportedOperationException();
  }

  @Override
  public int getBufferPercentage() {
    return exoPlayer.getBufferedPercentage();
  }

  @Override
  public int getCurrentPosition() {
    return exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
        : (int) exoPlayer.getCurrentPosition();
  }

  @Override
  public int getDuration() {
    return exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
        : (int) exoPlayer.getDuration();
  }

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

  @Override
  public void start() {
    exoPlayer.setPlayWhenReady(true);
  }

  @Override
  public void pause() {
    exoPlayer.setPlayWhenReady(false);
  }

  @Override
  public void seekTo(int timeMillis) {
    long seekPosition = exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
        : Math.min(Math.max(0, timeMillis), getDuration());
    exoPlayer.seekTo(seekPosition);
  }

}

(It appears that this question is still open). I would think you can change your icon within the override of your Activity onPause(), as follows:

Boolean isPlayIconOn;

isPlayIconOn=true; // set this flag to true when your icon is on "play"

@Override
public void onPause() {
    super.onPause();
    if (isPlayIconOn) {
       // insert code to change your icon to "pause" here;
       isPlayIconOn=false;
    }   
}

Similarly, you can change the icon back when your app is resuming, via the onResume() @override:

@Override
public void onResume() {
    super.onResume();
    if (!isPlayIconOn) {
       // insert code to change your icon to "play" here;
       isPlayIconOn=true;
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM