Mix_OpenAudio -- Initialize SDL_mixer.


#include "SDL_mixer.h"

int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize);

Note: You must not use AUDIO_S16, AUDIO_U16, AUDIO_S16LSB, or AUDIO_U16LSB. They are not portable, and SDL will not return an error code when they fail. The result will be a horrible staticy noise. You can usually use AUDIO_S16SYS, though not always. Future versions of SDL should take this parameter only as a hint, then read back the value that the OS (for example, OSS or ALSA) has chosen to use in case the desired audio type is not supported.


Mix_OpenAudio will initialize SDL_mixer if it is not yet initialized, see note. SDL_mixer may not be able to provide the exact specifications your provided, however it will automatically translate between the expected format and the real one. You can retrieve the real format using Mix_QuerySpec.

Return Value

Returns 0 on success, -1 on error.

Note: When already initialized, this function will not re-initialize SDL_mixer, nor fail. It will merely increment the number of times SDL_CloseAudio must be called to actually get it to uninitialize. This serves as a very simplistic method for multiple application components to use SDL_mixer without necessitating a great deal of inter-component awareness. Be warned however that in such a situation, the latest components to initialize SDL_mixer will probably not get the SDL_mixer settings they're expecting.


if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024) < 0)
    printf("Error initializing SDL_mixer: %s\n", Mix_GetError());

See Also

Mix_CloseAudio, Mix_QuerySpec

Mix_OpenAudio (last edited 2008-04-17 08:18:09 by localhost)