Mix_PlayChannel function in SDL_mixer

1.2k views Asked by At

As described in documentation, Mix_PlayChannel function have a 3 parameters:

  • channel: Channel to play on, or -1 for the first free unreserved channel.
  • chunk: Sample to play.
  • loops: Number of loops, -1 is infinite loops. Passing one here plays the sample twice (1 loop).

I don't understand the meaning of the channel parameter. What channel is referred to in the documentation?

1

There are 1 answers

0
Magehawk On

They are addressing mixed audio channels.

Since SDL_mixer is a multi-channel Audio Mixer Library, you can access multiple mixed audio channels at once and thus play multiple sounds independently at the same time. That's why it requires you to specify which channel to play it on. With the argument -1 it would just take the first free channel.

This is important to know when you try to stop only one of your playing sounds:

int Mix_HaltChannel(int channel);

This is how you can mix/allocate a certain amount of channels to work with:

int Mix_AllocateChannels(int amountChannels);

There is also a single music channel which can be addressed without specifying a channel which will also play independently:

int Mix_PlayMusic(Mix_Music *music, int loops);