本文介绍了ExoPlayer和开始/暂停/seekTo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ExoPlayer,而不是MediaPlayer,我似乎无法弄清楚...

I am trying to use ExoPlayer, as opposed to MediaPlayer and I can't seem to figure it out...

MediaPlayer具有.start()/.pause()命令...我只能按seekTo(1287)并且它会自动开始播放...

MediaPlayer has .start() / .pause() commands... and I can just seekTo(1287) and it automatically starts playing...

如何使用ExoPlayer执行此操作?我尝试做seekTo(1287),但是它在之后没有开始播放...之后,我还添加了.setPlayWhenReady(true),仍然没有运气...

How do I do this with ExoPlayer? I have tried to do seekTo(1287) but it doesn't start playing after... I have also added .setPlayWhenReady(true) after that, and still no luck...

我能够.stop() ...但是在那之后我无法让它再次开始播放,除非再次我.prepare() ...但我认为我不必在每次暂停之间都这样做玩.

I am able to .stop()... but I can't get it to start playing again after that unless I .prepare() again... but I don't think I should have to do that between every pause and play.

我正在使用与MediaController相对的我自己的控件和方法,例如在ExoPlayer演示中...我不太清楚控件的实现方式...

I am using my own controls and methods opposed to MediaController like in the ExoPlayer Demo... I can't quite see how the controls are implemented...

有人建议吗?

好的,我想出了暂停并开始...

OK, I figured out pause and start...

.setPlayWhenReady(true) // start
.setPlayWhenReady(false) // pause

但是我仍然在跟踪方面遇到问题... .seekTo间歇性工作...有时可以工作...但是其他时候我遇到此错误:

But I'm still having problems with the tracking... .seekTo works intermittently... sometimes it works... but other times I get this error:

E/AudioTrack: AudioTrack::set : Exit

(而且它只能进入缓冲区状态...尚未完全进入就绪"状态...

(and it only gets to the buffer state... doesn't quite get to "ready"...

推荐答案

的官方示例完全按照您的要求进行操作:

The official example of the PlayerControl in the ExoPlayer source code do exactly what you asked:

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

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

}

如果在搜索操作期间遇到奇怪的行为,则可能是由于您特定的流/文件类型所致.我建议您看看,并最终在Github上报告任何问题.

If you are experiencing strange behaviors during the seek operation, it may be due to you particular stream/file type. I can suggest you to take a look at the base implementation of the ExoPlayer and, eventually, report any issue on Github.

这篇关于ExoPlayer和开始/暂停/seekTo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:23
查看更多