Android MediaPlayer audio double speed playback adjustment playback speed problem

  • 2021-11-13 02:41:19
  • OfStack

At present, many audio and video App on the market have the function of double-speed playback, such as adjusting the playback speed to 0.5, 1.5, 2 times and so on.

Starting with Android API 23 (Android M), MediaPlayer supports adjusting playback speed.

The method used is setPlaybackParams, passing in a class PlaybackParams that represents the playback attribute.

This article describes how to use MediaPlayer to adjust the playback speed.

MediaPlayer. setPlaybackParams Description

The playback speed is set in the PlaybackParams object, and then this object is passed into setPlaybackParams.

setPlaybackParams is an native method.

If MediaPlayer is not ready (before prepared), calling this method does not change the state of MediaPlayer.

After MediaPlayer succeeds prepare, if the set speed is 0, it is equivalent to calling pause method; If the speed is not set to 0, it is equivalent to calling the start method.

Anomalous condition

Calling the setPlaybackParams method throws an IllegalStateException exception if the MediaPlayer is not initialized or has been released, i.e. in the Idle or End state.

If the incoming PlaybackParams is not supported, an IllegalArgumentException exception is thrown.

If the set speed is less than 0, an java. lang. IllegalArgumentException exception is thrown.

MediaPlayer. setPlaybackParams method example

Set the playback speed. Determine the current system version first.


private boolean setPlaySpeed(float speed) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    PlaybackParams params = mediaPlayer.getPlaybackParams();
    params.setSpeed(speed);
    mediaPlayer.setPlaybackParams(params);
    return true;
  }
  return false;
}

getPlaybackParams can get the current PlaybackParams object of MediaPlayer.

You can also add try catch to this method, and combine the returned boolean value to determine whether the speed setting was successful.


private boolean setPlaySpeed(float speed) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    try {
      PlaybackParams params = mediaPlayer.getPlaybackParams();
      params.setSpeed(speed);
      mediaPlayer.setPlaybackParams(params);
      return true;
    } catch (Exception e) {
      Log.e(TAG, "setPlaySpeed: ", e);
      return false;
    }
  }
  return false;
}

Reference code https://github.com/RustFisher/android-MediaPlayer

Velocity values included in PlaybackParams

When adjusting the playback speed of MediaPlayer, we used the PlaybackParams object. This class is also used by AudioTrack.

PlaybackParams contains a few attributes when playing. For example, speed is the playback speed.

PlaybackParams.setSpeed(float speed)

Incoming speed multiplier value. Will mark that the current setting has exceeded the speed.


public PlaybackParams setSpeed(float speed) {
  mSpeed = speed;
  mSet |= SET_SPEED;
  return this;
}
PlaybackParams.getSpeed()

Gets the set speed value. If the speed has not been set before, an IllegalStateException exception is thrown.


public float getSpeed() {
  if ((mSet & SET_SPEED) == 0) {
    throw new IllegalStateException("speed not set");
  }
  return mSpeed;
}

Summarize


Related articles: