Results 1 to 3 of 3

Thread: Loop music file

  1. #1

    Loop music file

    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...

    Code:
      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

  2. #2

    Loop music file

    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
    Code:
    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

  3. #3

    Loop music file

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •