Android中MediaPlayer的速度控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10849961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:13:14  来源:igfitidea点击:

Speed Control of MediaPlayer in Android

android

提问by Krishna Suthar

I am developing a player app and I am using MediaPlayerfor that.

我正在开发一个播放器应用程序,我正在使用MediaPlayer它。

Now, I want to change the speed of the playing track.

现在,我想改变播放曲目的速度。

I've seen so many apps with this functionality. How can I do this?

我见过很多具有此功能的应用程序。我怎样才能做到这一点?

采纳答案by Vipul Shah

The MediaPlayer does notprovide this feature but SoundPool has this functionality. The SoundPool class has a method called setRate (int streamID, float rate). If you are interested in the API have a look here.

MediaPlayer的不提供此功能,但拥有的Soundpool此功能。SoundPool 类有一个名为 的方法setRate (int streamID, float rate)。如果您对API感兴趣,请查看此处

This Snippet will work.

此代码段将起作用。

 float playbackSpeed=1.5f; 
 SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);

 soundId = soundPool.load(Environment.getExternalStorageDirectory()
                         + "/sample.3gp", 1);
 AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

 soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
 {
     @Override
     public void onLoadComplete(SoundPool arg0, int arg1, int arg2)
     {
         soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
     }
 });

回答by Shishir Gupta

Beginning API 23, MediaPlayer can set playback speed using this method.

从 API 23 开始,MediaPlayer 可以使用此方法设置播放速度。

Class MediaPlayer

public void setPlaybackParams (PlaybackParams params)Added in API level 23

Sets playback rate using PlaybackParams. Parameters params PlaybackParams: the playback params. Throws IllegalStateException if the internal player engine has not been initialized. IllegalArgumentException if params is not supported.

类媒体播放器

public void setPlaybackParams (PlaybackParams params)在 API 级别 23 中添加

使用 PlaybackParams 设置播放速率。参数 params PlaybackParams:播放参数。如果内部播放器引擎尚未初始化,则抛出 IllegalStateException。如果不支持 params,则为 IllegalArgumentException。

Sample code:

示例代码:

MediaPlayer mp = ...; //Whatever
float speed = 0.75f;     
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));

For API < 23, refer to Vipul Shah's answerabove (or below).

对于 API < 23,请参阅上面(或下面)Vipul Shah 的回答

回答by Bob

soundpool only supports relatively small sound effect files that can be preloaded. You will get heap overflows with any useful length music track.

soundpool 只支持比较小的可以预加载的音效文件。任何有用长度的音乐曲目都会导致堆溢出。

回答by Wallace Zhen

Now you could use

现在你可以使用

mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed)

mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed)

for API 23 and up!

对于 API 23 及更高版本!

回答by Kazekage Gaara

The MediaPlayerclass doesn't give this functionality. Instead use the SoundPoolclass. It has a method called setRate (int streamID, float rate). Read thisfor further info. Here is a sample codefor you to work with it.

MediaPlayer类不给这个功能。而是使用SoundPool类。它有一个方法叫做setRate (int streamID, float rate). 阅读本文了解更多信息。这是一个示例代码供您使用。

回答by Shailendra Yadav

PlaybackParams playbackParams = new PlaybackParams();
playbackParams.setSpeed(2);
playbackParams.setPitch(1);
playbackParams.setAudioFallbackMode(
    PlaybackParams.AUDIO_FALLBACK_MODE_DEFAULT);
mMediaPlayer.setPlaybackParams(playbackParams);