首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >MediaController播放图标将以编程方式更改为暂停图标

MediaController播放图标将以编程方式更改为暂停图标
EN

Stack Overflow用户
提问于 2015-08-24 20:41:23
回答 1查看 1.1K关注 0票数 1

我正在使用exoPlayer播放音乐,我正在通过安卓提供的mediaController控制音乐。我真正想要的是,任何外部事件,如电话呼叫,不仅应该暂停播放,还应该将图标更改为暂停。我已经设法使用telephoneManager应用程序接口来确定我是否在通话中,并且我可以在暂停时暂停mediaPlayBack,但我显然无法在此通话事件上更改播放图标以暂停。

MediaPlayerControl有一个pause()函数,我在暂停播放的地方调用它。playBack会暂停,但图标不会更改,如果有什么好方法,请告诉我

代码语言:javascript
运行
复制
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);
  }

}
EN

回答 1

Stack Overflow用户

发布于 2016-02-26 19:20:55

(这个问题似乎仍然悬而未决)。我认为您可以在Activity onPause()的覆盖中更改图标,如下所示:

代码语言:javascript
运行
复制
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更改回图标:

代码语言:javascript
运行
复制
@Override
public void onResume() {
    super.onResume();
    if (!isPlayIconOn) {
       // insert code to change your icon to "play" here;
       isPlayIconOn=true;
    }   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32182605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档