This is how i do it, it may be very useful for someone.I find it very usefull.I think that this can be writen even better, but it suits me, and i think in 99.0% of time there will be safe to use as is, without producing errors or making troubles.I`m not sure if there is snippet with something like this, so enjoy if there isn`t.
Used plugins :
Matrix1 Plugin
Music Snippet - Last test and changes in this snippet shows no problems with it.
sync on:sync rate 60
type musics
ID as integer
playing_state as boolean
endtype
dim music(99) as musics
do
cls
text 0,60,"FreeMusicID: "+str$(FreeMusicID)
for x = 0 to 9
text 0,80+(x*20),"musics("+str$(x)+") = "+str$(music(x).ID)
if music(x).ID>0
text 100,80+(x*20)," music playing: "+str$(music playing(music(x).ID))+" music playing state: "+str$(music(x).playing_state)
endif
next x
if mouseclick()=1 and press = 0
press = 1
play_music()
endif
if mouseclick()=0 then press = 0
delete_music()
sync
loop
function play_music()
FreemusicID = find free music()
load music "Media/Sounds/death"+str$(rnd(4))+".mp3",FreeMusicID
if FreeMusicID => music(99).ID
music(99).ID = FreeMusicID
sort array music().ID,-1
endif
for ID = 0 to 99
if music(ID).ID > 0
if music(ID).playing_state = 0
play music music(ID).ID
music(ID).playing_state = 1
endif
endif
next ID
endfunction
function delete_music()
for ID = 0 to 99
if music(ID).ID>0
if music exist(music(ID).ID) = 1
if music playing (music(ID).ID) = 0
music(ID).playing_state = 0
endif
endif
endif
next ID
for ID = 0 to 99
if music(ID).ID>0
if music exist(music(ID).ID) = 1
if music playing (music(ID).ID) = 0 and music(ID).playing_state = 0
delete music music(ID).ID
music(ID).ID = 0
endif
endif
endif
next ID
endfunction
Sound Snippet - Issue: Doesen`t free up Virtual Memmory!
sync on:sync rate 60
type sounds
ID as integer
playing_state as boolean
endtype
dim sound(99) as sounds
do
cls
text 0,60,"FreeSoundID: "+str$(FreeSoundID)
for x = 0 to 9
text 0,80+(x*20),"sounds("+str$(x)+") = "+str$(sound(x).ID)
if sound(x).ID>0
text 100,80+(x*20)," sound playing: "+str$(sound playing(sound(x).ID))+" sound playing state: "+str$(sound(x).playing_state)
endif
next x
if mouseclick()=1 and press = 0
press = 1
play_sound()
endif
if mouseclick()=0 then press = 0
delete_sound()
sync
loop
function play_sound()
FreeSoundID = find free sound()
load sound "Media/Sounds/death"+str$(rnd(4))+".ogg",FreeSoundID
if FreeSoundID => sound(99).ID
sound(99).ID = FreeSoundID
sort array sound().ID,-1
endif
for ID = 0 to 99
if sound(ID).ID > 0
if sound(ID).playing_state = 0
play sound sound(ID).ID
sound(ID).playing_state = 1
endif
endif
next ID
endfunction
function delete_sound()
for ID = 0 to 99
if sound(ID).ID>0
if sound exist(sound(ID).ID) = 1
if sound playing (sound(ID).ID) = 0
sound(ID).playing_state = 0
endif
endif
endif
next ID
for ID = 0 to 99
if sound(ID).ID>0
if sound exist(sound(ID).ID) = 1
if sound playing (sound(ID).ID) = 0 and sound(ID).playing_state = 0
delete sound sound(ID).ID
sound(ID).ID = 0
endif
endif
endif
next ID
endfunction
Thanks myself for not being lazy!
EDIT: I made an update to the code.Past state of it didn`t unload the virtual memmory and sometimes there is music stuck in the array with state of playing but it does not.So there is new function included force_free, it check the sound counter.Perhaps nobody need 20+musics/sounds at the same time so it means there is need of clear on stucked sounds/musics.Unfortunately it`ll stop all sounds playing at this time.I`ll update when figure out how to prevent this.
Also i remove "backdrop on" command and added cls instead, i write it by habit, it realesed arround 150-180k virtual memory.
EDIT2: I made sound version of the snippet.It`s a bit diferent, there is no sound stuck in the array anymore.There is just virtual memory issue, it doesen`t free when new file is deleted.I`ll work on this.It maybe caused by
find free comand
EDIT3: Update of music version, it`s exact copy of sound one, using UDT array and so on,but it doesn`t have the virtual memory issue in it.Force free function is removed.Test shows that is 100% bug free, for now...
EDIT4: I forget to mention something.When you use the code above you need to call your sounds like this:
FreeMusicID = find free music()
load music "Media/Sounds/mysound.mp3",FreeMusicID
or like this, because eventualy you need volume option for your game:
FreeMusicID = find free music()
set music volume FreeMusicID,volume
load music "Media/Sounds/mysound.mp3",FreeMusicID
and you need to call the function play_music like this:
Enjoy!
Where there is a will, there is a way.
I often edit my posts, that`s who i am