Speed Control of MediaPlayer in Android

前端 未结 6 1357
别那么骄傲
别那么骄傲 2020-11-28 09:41

I am developing a player app and I am using MediaPlayer for that.

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

I\'ve seen so many apps with this fu

6条回答
  •  时光取名叫无心
    2020-11-28 09:49

    The MediaPlayer does not provide 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.

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

提交回复
热议问题