倍速播放

倍速播放几乎是现在主流的视频App必备功能,最近播放器又在加需求了,顺便研究了一下。其实也简单,EXOplayer底层已经提供了方法,只需调用即可,比较简单,直接扔代码:

SimpleExoPlayer simpleExoPlayer = this.getExoPlayer();
if(simpleExoPlayer != null) {
    PlaybackParameters playbackParameters = new PlaybackParameters(speed, 1.0F);
    simpleExoPlayer.setPlaybackParameters(playbackParameters);
}

PlaybackParameters源码如下:

package com.google.android.exoplayer2;

/**
 * The parameters that apply to playback.
 */
public final class PlaybackParameters {

  /**
   * The default playback parameters: real-time playback with no pitch modification.
   */
  public static final PlaybackParameters DEFAULT = new PlaybackParameters(1f, 1f);

  /**
   * The factor by which playback will be sped up.
   */
  public final float speed;

  /**
   * The factor by which the audio pitch will be scaled.
   */
  public final float pitch;

  private final int scaledUsPerMs;

  /**
   * Creates new playback parameters.
   *
   * @param speed The factor by which playback will be sped up.
   * @param pitch The factor by which the audio pitch will be scaled.
   */
  public PlaybackParameters(float speed, float pitch) {
    this.speed = speed;
    this.pitch = pitch;
    scaledUsPerMs = Math.round(speed * 1000f);
  }

  /**
   * Scales the millisecond duration {@code timeMs} by the playback speed, returning the result in
   * microseconds.
   *
   * @param timeMs The time to scale, in milliseconds.
   * @return The scaled time, in microseconds.
   */
  public long getSpeedAdjustedDurationUs(long timeMs) {
    return timeMs * scaledUsPerMs;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
      return false;
    }
    PlaybackParameters other = (PlaybackParameters) obj;
    return this.speed == other.speed && this.pitch == other.pitch;
  }

  @Override
  public int hashCode() {
    int result = 17;
    result = 31 * result + Float.floatToRawIntBits(speed);
    result = 31 * result + Float.floatToRawIntBits(pitch);
    return result;
  }

}
GitHub 加速计划 / ex / ExoPlayer
1
1
下载
最近提交(Master分支:4 个月前 )
dd430f70 - 1 年前
c00f90aa (cherry picked from commit 5b2b7f4e6147678dc56ce28826682ba21c4c9508) 1 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐