简体   繁体   中英

Changing playback speed in Exoplayer

I'm looking to implement an audio player with variable speed playback (1.0x, 1.25x, 1.5x) like typical audiobook players currently on the market do. I would like to use Google's Exoplayer library as my audioplayer library however they don't appear to support variable speed playback. Any ideas on how to implement this, or any extensions that do support this?

The function setPlaybackSpeed() was removed and now you set the playback speed via:

    PlaybackParameters param = new PlaybackParameters(speed);
    mExoPlayer.setPlaybackParameters(param);

speed is a float number. Normal speed is 1f and double the speed would be 2f .

All you need is https://github.com/waywardgeek/sonic/blob/master/Sonic.java

If you look at MediaCodecAudioTrackRenderer.java , you can get the output buffer (decoded by MediaCodec) from ExoPlayer in method processOutputBuffer and process it through Sonic.java accordingly before sending it to AudioTrack .

Following document explains how to use libsonic https://github.com/waywardgeek/sonic/blob/master/doc/index.md

Kotlin Extension Solution

Make it easy to access and set this reliably across your app in Kotlin

// To set
player.playbackSpeed = 2f

var SimpleExoPlayer.playbackSpeed: Float
    get() = playbackParameters?.speed ?: 1f
    set(speed) {
        val pitch = playbackParameters?.pitch ?: 1f
        playbackParameters = PlaybackParameters(speed, pitch)
    }

Try This

I have followed all the answer nothing worked, so i have tried the below solution, it works for me

PlaybackParams param = new PlaybackParams();
param.setSpeed(1f);// 1f is 1x, 2f is 2x 
exoPlayer.setPlaybackParams(param);

in Exoplayer version 2.14.0

Pass the float value directly like this.

simpleExoPlayer?.setPlaybackSpeed(1.0f)

You should take a look this project, which was very useful for me: https://github.com/AmrMohammed89/exoplayer2.4.0_speedup

Inside SimpleExoPlayer, I implemented the next methods:

 private final ExoPlayer player;
private float playbackSpeed;

float SPEED_NORMAL = 1f;
float SPEED_MEDIUM = 1.5f;
float SPEED_HIGH = 2f;


 @Override
public float getPlaybackSpeed() {
    return playbackSpeed;
}

@Override
public void setPlaybackSpeed(float speed) {
    playbackSpeed = speed;
    player.setPlaybackSpeed(speed);
}

@Override
public void changePlaybackSpeed() {
    if (playbackSpeed == SPEED_MEDIUM) {
        player.setPlaybackSpeed(SPEED_HIGH);
        playbackSpeed = SPEED_HIGH;
    } else if (playbackSpeed == SPEED_HIGH) {
        player.setPlaybackSpeed(SPEED_NORMAL);
        playbackSpeed = SPEED_NORMAL;
    } else {
        player.setPlaybackSpeed(SPEED_MEDIUM);
        playbackSpeed = SPEED_MEDIUM;
    }
}

I set and saved the speed that way due to there was a bug when I tried to get the last saved speed. So follow that mechanism and it will work perfectly.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM