[SDL] playing audio wave file

Bill Kendrick nbs at sonic.net
Tue Sep 12 16:51:25 PDT 2006


On Wed, Sep 13, 2006 at 12:55:41AM +0200, Michi wrote:
> Hi,
> I playing a wav successfully; but I'm wondering: how do I play the file 
> in a loop? (I'd like when file reach the end, automatically jump to 
> begin and restart). Is it possible?

Let's assume you're using SDL_mixer, since that's what most people do...


If you look here:
  http://www.libsdl.org/cgi/docwiki.cgi/Mix_5fPlayChannel

It says:
  int Mix_PlayChannel (int channel, Mix_Chunk *chunk, int loops);
  ...
  The chunk will be looped loops times. (The total number of times
  played will be loops+1.)

See also:
  http://jcatki.no-ip.org/SDL_mixer/SDL_mixer.html#SEC24


Depending on how you're coding your app, you may also want to use:

  int Mix_Playing(int channel)
  Tells you if channel is playing, or not.
  http://jcatki.no-ip.org/SDL_mixer/SDL_mixer.html#SEC34

to simply play the sound again, when you noticed it has stopped,

or:

  void Mix_ChannelFinished(void (*channel_finished)(int channel))
  When channel playback is halted, then the specified channel_finished
  function is called. The channel parameter will contain the channel
  number that has finished.
  http://jcatki.no-ip.org/SDL_mixer/SDL_mixer.html#SEC33

to have SDL_mixer call a function immediately when your sound has stopped.
Note that you cannot actually restart the sound _within_ the callback.
You'll want to set a global flag, or fire off a user event.


It helps to look at the docs. ;)

-bill!




More information about the SDL mailing list