Ferdari Active Member Licensed User Longtime User Jan 15, 2017 #1 Hello, I was trying to play 2 or more sound in different interfaces generated Dynamically of MediaPlayer, but im getting: Code: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 My code: Code: Sub Play_Sound(Snd As String) Dim media As String Dim mps() as MediaPlayer For i = 0 To 2 -1 Log(i) If Not(mps(i).IsPlaying) Then mps(i).Initialize() media="sounds/" & Snd mps(i).Load(File.DirAssets,media) mps(i).Play Exit End If Next End Sub Im newbie using MediaPlayers, i dont know how to create dynamically MediaPlayers and use it, without handling too much and limited IFs like Code: if mps1.IsPlaying then mps2.Load(...) mps2.Play And so on mediaplayer3,4,5,6... Can anyone help me? Last edited: Jan 15, 2017
Hello, I was trying to play 2 or more sound in different interfaces generated Dynamically of MediaPlayer, but im getting: Code: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 My code: Code: Sub Play_Sound(Snd As String) Dim media As String Dim mps() as MediaPlayer For i = 0 To 2 -1 Log(i) If Not(mps(i).IsPlaying) Then mps(i).Initialize() media="sounds/" & Snd mps(i).Load(File.DirAssets,media) mps(i).Play Exit End If Next End Sub Im newbie using MediaPlayers, i dont know how to create dynamically MediaPlayers and use it, without handling too much and limited IFs like Code: if mps1.IsPlaying then mps2.Load(...) mps2.Play And so on mediaplayer3,4,5,6... Can anyone help me?
Erel B4X founder Staff member Licensed User Longtime User Jan 15, 2017 #2 I don't see how this code can work. You are creating an empty array of MediaPlayers. I guess that mps should have been a global variable. BTW, if the sounds are short then it is better to use SoundPool. Upvote 0
I don't see how this code can work. You are creating an empty array of MediaPlayers. I guess that mps should have been a global variable. BTW, if the sounds are short then it is better to use SoundPool.
Ferdari Active Member Licensed User Longtime User Jan 15, 2017 #3 how do i need to fill the array in order to work? i tried almost everything Thanks Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Jan 15, 2017 #4 Code: Dim mps(2) As MediaPlayer For i = 0 to mps.Length - 1 mps(i).Initialize2("") Next Upvote 0