Android Question random play mp3 file

tufanv

Expert
Licensed User
Longtime User
Hello

in an old topic, member pluton has specified a code to play random mp3 files that working perfectly:

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

I just want the player not play the same files 2 times in a row. for example laz_2 will not play again(Laz_1 ,3 or 4 must play) if previous played file was laz_2

ty
 

susu

Well-Known Member
Licensed User
Longtime User
Something like this:
- Create a list of your mp3 files.
- Play a file from list randomly.
- Delete it from the list after play.
- Play another file from list.

B4X:
Dim playlist As List
Dim randomsong As Int

'In Activity_Create Sub
If playlist.IsInitialized = False Then
    playlist.Initialize
    playlist.Add("1.mp3")
    playlist.Add("2.mp3")
    playlist.Add("3.mp3")
    playlist.Add("4.mp3")
End If
'------------------------------

'Sub to get new random song name
If playlist.Size > 0 Then
        randomsong = Rnd(0, playlist.Size)
        Log(playlist.Get(randomsong))
        'Call another Sub to play this song
        playlist.RemoveAt(randomsong)
Else
        Log("No more song to play")
End If
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
But i want to play the same file again i just dont want to play them again in a row. for example 1.mp3 will play then 1 will not be able to play but 2 , 3, or 4 will. Lets say 3 was played then 3 will not play again but 1,2,4 will be played. So i cant delete it from the list
 
Upvote 0

eps

Expert
Licensed User
Longtime User
But i want to play the same file again i just dont want to play them again in a row. for example 1.mp3 will play then 1 will not be able to play but 2 , 3, or 4 will. Lets say 3 was played then 3 will not play again but 1,2,4 will be played. So i cant delete it from the list

Either see my link, or where it says no more songs to play, get them again.. or have to arrays, one main and one work one and only remove from one of them. The choices are yours.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Make a shadow array the same size as the playlist. Once the song is played, change the value in the shadow array to mark the song as being played. You would then check the shadow array to see if the song has already been played and select another random element if it has. Once the array is filled with "played" songs, clear it and start over. If you have a lot of songs, you may need to add some code to limit the number of iterations to find an un-played element and just look for the first available element to play.

I did something similar for a slideshow app but I pre-randomized the shadow array. The user would start the slideshow and the screen would blank and show a progress bar showing the status of the randomization. It looked better than having a blank screen or a "please wait" notice. I would then start at element 0 (which contained a pointer to a random image from the list) and run to the end of the array.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See this example.

First a list (here A to Z) is genrated (could be a mp3-list)
Then this list is shuffled
when you play first one of the list (filetoplay = GetNextFromPL) the first item from list is removed and added to the end of list...
Now you have an shuffled endless playlist till you click on shuffle again...

BUT be adviced that Chuck Norris will be able to count this playlist. :D
 

Attachments

  • playlistshuffle.zip
    7.5 KB · Views: 400
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi all,

probably I enterely missed the point, but if the only requirement is to avoid that next item differs from current one, would it not suffice to record current playing index and then compare it with the newly generated one from a call to the random function, repeating the random generation until they differ?
In pseudo code:
B4X:
Sub Process_Gobals
Dim CurPlayingIndex as int
...
end sub

'generate next item to play different from last used
Sub NextItem2Play
Dim Next2Play as int
repeat
   Next2Play=rnd(1,4)
until Next2Play <> CurPlayIndex
CurPlayIndex = Next2Play
return CurPlayIndex

Umberto
 
Upvote 0
Top