倍速播放

倍速播放几乎是现在主流的视频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
0
1
下载
最近提交(Master分支:2 个月前 )
dd430f70 - 1 年前
c00f90aa (cherry picked from commit 5b2b7f4e6147678dc56ce28826682ba21c4c9508) 1 年前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐