Android mediaplayer audio playback speed adjustment

Many audio and video apps on the market now have the function of double speed playback, such as adjusting the playback speed to 0.5, 1.5, 2x, etc.

Starting with Android API 23 (Android m), mediaplayer supports adjusting the playback speed.

The method used is setplaybackparams, and a class playbackparams representing playback properties is passed in.

This article describes how to adjust the playback speed using mediaplayer.

Mediaplayer.setplaybackparams description

The playback speed is set in the playbackparams object, and then this object is passed into setplaybackparams.

Setplaybackparams is a native method.

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

After mediaplayer successfully prepares, if the speed is set to 0, it is equivalent to calling the pause method; If the speed is not set to 0, it is equivalent to calling the start method.

Abnormal condition

If mediaplayer is not initialized or has been released, that is, it is in idle or end state, calling setplaybackparams method will throw IllegalStateException.

If the incoming playbackparams is not supported, an illegalargumentexception is thrown.

If the setting speed is less than 0, a java.lang.illegalargumentexception is thrown.

Mediaplayer.setplaybackparams method example

Set the playback speed. First judge the current system version.

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 judge whether the speed setting is successful in combination with the returned Boolean value.

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

The speed value contained in playbackparams

When adjusting the playback speed of mediaplayer, we used the playbackparams object. Audiotrack also uses this class.

Playbackparams contains some properties during playback. For example, speed is the playback speed.

PlaybackParams.setSpeed(float speed)

Pass in the speed magnification value. It 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 is thrown.

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

summary

The above is the Android mediaplayer audio playback speed adjustment introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>