Android mediaplayer audio playback speed adjustment

Time:2020-8-11

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, 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 representing the playback property.

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

MediaPlayer.setPlaybackParams explain

The playback speed is set in the playbackparams object, and then the 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 setting speed is not 0, it is equivalent to calling the start method.

Abnormal situation

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

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

If the set speed is less than 0 java.lang.IllegalArgumentException Abnormal.

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 a try catch to this method to determine whether the setting speed is successful or not by combining 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)

Input velocity multiplier value. The current set speed is marked.


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 media player audio playback speed adjustment problem introduced by the editor. I hope it can help you. If you have any questions, please leave me a message, and the editor will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]