In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Android MediaPlayer audio speed playback and adjust the playback speed of the example analysis, the article is very detailed, has a certain reference value, interested friends must read!
Many audio and video apps on the market now have the function of multi-speed playback, such as adjusting the playback speed to 0.5, 1.5, 2 times, etc.
Starting with Android API 23 (Android M), MediaPlayer supports adjusting playback speed.
The method used is setPlaybackParams, passing in a class PlaybackParams that represents playback properties.
This article describes how to adjust playback speed using MediaPlayer.
MediaPlayer.setPlaybackParams Description
The playback speed is set in the PlaybackParams object, which is passed into setPlaybackParams.
setPlaybackParams is a native method.
If MediaPlayer is not prepared (before prepared), calling this method does not change MediaPlayer's state.
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 situation
If MediaPlayer is not initialized or has been released, that is, in Idle or End state, calling the setPlaybackParams method throws an IllegalStateException exception.
IllegalArgumentException thrown if the incoming PlaybackParams are not supported.
If the setting speed is less than 0, a java.lang.IllegalArgumentException is thrown.
MediaPlayer.setPlaybackParams method example
Set playback speed. Determine 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 gets the current PlaybackParams object for MediaPlayer.
You can also add try catch to this method, and combine it with the boolean value returned 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
PlaybackParams contains velocity values
When adjusting MediaPlayer playback speed, we used PlaybackParams objects. AudioTrack also uses this class.
PlaybackParams contains properties for playback. For example, speed is playback speed.
PlaybackParams.setSpeed(float speed)
Pass in the speed multiplier value. The current setting is marked as exceeding speed.
public PlaybackParams setSpeed(float speed) { mSpeed = speed; mSet |= SET_SPEED; return this;}PlaybackParams.getSpeed()
Gets the speed value that has been set. IllegalStateException thrown if speed has not been set before.
public float getSpeed() { if ((mSet & SET_SPEED) == 0) { throw new IllegalStateException("speed not set"); } return mSpeed;} The above is "Android MediaPlayer audio speed playback and adjust playback speed example analysis" All the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.