2

I'm trying to play music using SDL2_Mixer library in Ubuntu 18.04.

I succeed to play .wav file loop forever as below code.

#define ERROR_BEEP "../audio/beep.wav"

#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
    
    void main()
    {
        SDL_AudioSpec wavSpec;
        
        SDL_Init(SDL_INIT_AUDIO)
        Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) >= 0)
        
        SDL_LoadWAV(ERROR_BEEP, &wavSpec, &wavBuffer, &wavLength);
    
        Mix_Music *now_playing_= Mix_LoadMUS(ERROR_BEEP);
        Mix_PlayMusic(now_playing_, -1);

        SDL_Delay(10000);
        Mix_HaltMusic();
        Mix_FreeMusic(now_playing_);
        now_playing_=NULL;
    }

My beep.wav plays well repeatedly. However, the beep file is played immediately and the alarm sound is too loud. (ex: beep!beep!beep!)

Is there any way to put sleep between beep repeating loop? (ex: beep!...beep!...beep!...)

Biffen
  • 6,249
  • 6
  • 28
  • 36
djowlrid
  • 71
  • 1
  • 5
  • 1
    You could read the sound without loop but repeatedly from a timer callback and set the timer delay to the value you want (for instance 1 second). Or you could do the same with a loop and a sleep as you suggested but, in that case, you should run your loop from a background thread to avoid blocking the main thread. – dspr Oct 19 '20 at 07:00
  • @dspr Thank you for your comment. :) It was solved using thread in a similar way. I changed to play music once. Also I checked whether to play with Mix_PlayingMusic(), and changed to play again after SDL_Delay(1000) if it is not played. – djowlrid Oct 20 '20 at 05:37

0 Answers0