PDA

View Full Version : Loop music file



CheGueVerra
13-01-2008, 11:43 PM
So I wanted to add a song to my first game, and after a bit of research and hard work, it works.


The only trouble I have left is that the music doesn't loop...



if SDL_Init&#40; SDL_INIT_AUDIO or SDL_INIT_VIDEO &#41; <> 0 then
halt;

if Mix_OpenAudio&#40;44100, AUDIO_S16LSB, 2, 4096&#41; = -1 then
halt;

Music &#58;= Mix_LoadMUS&#40;'CrazyAceFighter.ogg'&#41;;
if Music = nil begin
SDL_Quit;
halt;
end;

=== -1 = Loops infinite ... not working ... tried 2 &#40;plays twice not working&#41;
if Mix_PlayMusic&#40;Music, -1&#41; = -1 then
begin
SDL_Quit;
halt;
end;

... Event Loop here ...

Mix_CloseAudio;
SDL_Quit;


The music plays once, no other commands are used...

Where did I go wrong

CheGueVerra

CheGueVerra
14-01-2008, 01:14 AM
I found a work around but I still don't understand why the loop parameter doesn't work for Mix_PlayMusic(Music, loops)...


My Solution


procedure LoadPlayMusic;
begin
if Mix_OpenAudio&#40;44100, AUDIO_S16LSB, 2, 4096&#41; = -1 then
begin
SDL_Quit;
halt;
end;

Music &#58;= Mix_LoadMUS&#40;'CrazyChe.ogg'&#41;;
if Music = nil then
begin
SDL_Quit;
halt;
end;

if Mix_PlayMusic&#40;Music, 0&#41; = -1 then
begin
SDL_Quit;
halt;
end;
end;

procedure MusicFinished;
begin
Mix_HaltMusic;
Mix_FreeMusic&#40;Music&#41;;
LoadPlayMusic;
end;

begin
if SDL_Init&#40; SDL_INIT_AUDIO or SDL_INIT_VIDEO &#41; <> 0 then
halt;
// Set the title bar in environments that support it
SDL_WM_SetCaption&#40; 'Sprite Engine &#58; Shooting', nil &#41;;

LoadPlayMusic;
Mix_HookMusicFinished&#40;@MusicFinished&#41;;

begin



When the Mixer, detects the end of playing the music file, it will call the musicFinished Method,
which calls the LoadPlayMusic Method once it frees the previous Music Pointer.

This will do for now, but would like some help with Mix_PlayMusic, so it will loop by itself...


CheGueVerra

CheGueVerra
26-01-2008, 02:28 PM
It seems that my workaround for looping crashes when executing on other machines,
but not in debug mode, I'm trying to figure what's going on...


Anyone have some tips, I've isolated the music loop in a single project, will try to find the problem.

ChegueVerra