Android Question How to set MediaPlayer Play Speed?

mywmshow

Member
Excuse me, I use Google Translate for this issue.
I want to set MediaPlayer Play Speed. What should I do?
I searched this code online but it didn't work. Hope to get everyone's help, thank you very much!

MediaPlayer Play Speed:
Process_Globals
    Dim MP As MediaPlayer
End Sub


Sub Activity_Create(FirstTime As Boolean)
    MP.Initialize2("MP")
End Sub

Sub BtnTest_Click
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("setPlaySpeed",Array(MP,0.5f))
End Sub

#if JAVA

import android.media.MediaPlayer;
import android.media.PlaybackParams;

public boolean setPlaySpeed(MediaPlayer tmp_mp,float speed) {
    try {
        PlaybackParams params = tmp_mp.getPlaybackParams();
        params.setSpeed(speed);
        tmp_mp.setPlaybackParams(params);
        return true;
    } catch (Exception e) {
        return false;
    }
}

#end if
 

Attachments

  • 3JR7Z[48QU2N`H2D29@69GI.png
    3JR7Z[48QU2N`H2D29@69GI.png
    19.9 KB · Views: 137
  • 3JR7Z[48QU2N`H2D29@69GI.png
    3JR7Z[48QU2N`H2D29@69GI.png
    19.9 KB · Views: 136

stevel05

Expert
Licensed User
Longtime User
You need to pass the native MediaPlayer object rather than the B4a Wrapped Mediaplayer Object

B4X:
Sub Activity_Create(FirstTime As Boolean)
    MP.Initialize2("MP")
    MP.Load(File.DirAssets,"BoogieWonderland.mid") 'whatever file you want to play

    Dim MPJO As JavaObject = MP
    MPJO = MPJO.GetField("mp") 'Get the native Mediaplayer object

    Dim jo As JavaObject
    jo.InitializeContext
    Dim Speed as Float = 0.5
    jo.RunMethod("setPlaySpeed",Array(MPJO,Speed))

    MP.Play
End Sub

This also works with JavaObject:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    MP.Initialize2("MP")
    MP.Load(File.DirAssets,"BoogieWonderland.mid") 'whatever file you want to play


    Dim jo As JavaObject = MP
    jo = jo.GetField("mp") 'Get the native Mediaplayer object

    Dim PP As JavaObject = jo.RunMethod("getPlaybackParams",Null)
    Dim Speed as Float = 0.5
    PP.RunMethod("setSpeed",Array(Speed))
    jo.RunMethod("setPlaybackParams",Array(PP))

    MP.Play
End Sub
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
hi,
how I can use the speed rate for mp4 video
thanks
Start a new thread for this question, it is likely to get missed by those that may know in this old thread.
 
Upvote 0
Top