簡體   English   中英

MediaController播放圖標以編程方式更改為暫停圖標

[英]MediaController Play Icon to change to Pause Icon programmatically

我使用exoPlayer播放音樂,我通過android提供的mediaController控制音樂。 我真正想要的是任何外部事件(例如電話呼叫)都不僅應暫停播放,還應將圖標更改為暫停。 我已經設法使用電話管理器Api查找我是否在通話中,並且能夠在“暫停”時暫停mediaPlayBack,但顯然無法更改播放圖標以在此通話事件上暫停。

MediaPlayerControl有一個pause()函數,在暫停播放的地方調用該函數。 播放暫停,但圖標未更改,請告訴我是否有任何好的方法

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);
  }

}

(看來這個問題仍未解決)。 我認為您可以在Activity onPause()的覆蓋范圍內更改圖標,如下所示:

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;
    }   
}

同樣,您可以在應用恢復時通過onResume()@override將圖標改回來:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM