Here it is
type music_type
file as string
now as integer
nxt as integer
playing as integer
musiccount as integer
filecount as integer
endtype
global music_control as music_type
global dim music(0) as string
innitialize_music()
repeat
process_music()
until keystate(1)
end
function process_music()
randomize timer()
if music_control.playing = 1
if not music playing(music_control.now)
play music music_control.nxt
delete music music_control.now
load music music(rnd(music_control.filecount - 1) + 1), music_control.now
music_control.playing = 2
endif
else
if not music playing(music_control.nxt)
play music music_control.now
delete music music_control.nxt
load music music(rnd(music_control.filecount - 1) + 1), music_control.nxt
music_control.playing = 1
endif
endif
endfunction
function innitialize_music()
randomize timer()
directory$ = "music\"
set dir directory$
find first
counter = 0
while get file type() > -1
file$ = get file name$()
filetype$ = right$(file$, 3)
filetype$ = lower$(filetype$)
if filetype$ = "mp3"
inc counter, 1
array insert at bottom Music(0)
music(counter) = Directory$ + file$
endif
find next
endwhile
letters = len(get dir$())
set dir left$(get dir$(), letters - 6)
music_control.filecount = counter
music_control.playing = 1
music_control.musiccount = 1
music_control.now = free_music()
load music music(rnd(music_control.filecount - 1) + 1), music_control.now
music_control.nxt = free_music()
load music music(rnd(music_control.filecount - 1) + 1), music_control.nxt
play music music_control.now
endfunction
function free_music()
counter = 1
while music exist(counter)
inc counter, 1
endwhile
endfunction counter
Explanation:
Store all your mp3 music in a subfolder called 'music\'
The innitialize function scans the 'music\' directory for all mp3 files and stores the file name of each one in the music(0) array, it is then from this array that music is randomly loaded and played during your game.
Just pop 'Process_music()' into all game loops so that music can be played non stop.
RealUnReal