Get the App
SLTechnology News&Howtos  ›  Development  › 

How to adjust the playback speed using MediaPlayer

Shulou Source: shulou.com Published: 2022-06-02 17:57:08 09月22日 Update

This article focuses on "how to use MediaPlayer to adjust the playback speed", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use MediaPlayer to adjust the playback speed.

MediaPlayer.setPlaybackParams description

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

SetPlaybackParams is a native method.

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

After MediaPlayer succeeds in prepare, if the speed is set to 0, the pause method is called; if the speed is not set to 0, the start method is called.

Abnormal situation

If the MediaPlayer is not initialized or has been released, that is, it is in the Idle or End state, calling the setPlaybackParams method throws an IllegalStateException exception.

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

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

Example of MediaPlayer.setPlaybackParams method

Set the playback speed. Judge 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 is successful or not.

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;}

The speed value contained in PlaybackParams

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

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

PlaybackParams.setSpeed (float speed)

Pass in the speed multiplier value. Marks that the speed is currently set.

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;}

At this point, I believe you have a deeper understanding of "how to use MediaPlayer to adjust the playback speed". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Speed method adjustment object success content state learning practical deeper meeting mark magnification interest practicality practicality that is attributes situation simple operation time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Microsoft Docker Linux