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