How can i randomly play files?

itbank

New Member
Licensed User
Longtime User
I know directory where files are stored, but how to open them randomly?
 

pluton

Active Member
Licensed User
Longtime User
You didn't say what are you playing but i assume a .mp3

One way you can do like this.

I have added some test mp3 with name:

laz_1.mp3
laz_2.mp3
laz_3.mp3
laz_4.mp3

And the code goes like this (I commented it for you)

B4X:
Sub random_Click

   Dim i As Int
   i = Rnd(1,4) ' random number from 1 to 4
   
   mediaplayer1.Load(File.DirAssets, "laz_"&i&".mp3") ' load our mp3 with random number
   Timer1.Initialize("timer1", 1000)
   MediaPlayer1.Play ' media is playing
   timer1.Enabled = True
   
   ToastMessageShow("Now is playing laz_"&i&".mp3", False) ' just check to see what is playing
   

        
End Sub
 
Upvote 0

itbank

New Member
Licensed User
Longtime User
replyreply

I dont know file names, i use chooser to get Dir, after i want to play all mp3 in this Dir. How can i list files. I tried to toast list but i get exception
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
I'd like to get and play a random audio file (or an image) stored in a folder of my website. Is there a way ?
I think File.ListFiles need local folder.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
how can we avoid playing the same file 2 times in a row ?


You didn't say what are you playing but i assume a .mp3

One way you can do like this.

I have added some test mp3 with name:

laz_1.mp3
laz_2.mp3
laz_3.mp3
laz_4.mp3

And the code goes like this (I commented it for you)

B4X:
Sub random_Click

   Dim i As Int
   i = Rnd(1,4) ' random number from 1 to 4
  
   mediaplayer1.Load(File.DirAssets, "laz_"&i&".mp3") ' load our mp3 with random number
   Timer1.Initialize("timer1", 1000)
   MediaPlayer1.Play ' media is playing
   timer1.Enabled = True
  
   ToastMessageShow("Now is playing laz_"&i&".mp3", False) ' just check to see what is playing
  

       
End Sub
 
Upvote 0
Top