倍速播放
倍速播放几乎是现在主流的视频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;
}
}