AudioSDL 3.2

SDL_OpenAudioDevice

Opens an audio device you can bind several streams to. SDL mixes them for you.

Signature

SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);

How it works

Use this when you need more than one sound at a time: one stream for music, one for effects, one per voice. Bind each stream with SDL_BindAudioStream and SDL adds their outputs together.

Unlike SDL_OpenAudioDeviceStream, a device opened here starts UNpaused: sound flows as soon as a bound stream has data. It returns a new logical device ID; close it with SDL_CloseAudioDevice.

Parameters

devidSDL_AudioDeviceID

SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, or a specific device ID.

specconst SDL_AudioSpec *

A format hint for the device, or NULL for sensible defaults. Streams convert anyway.

Return value

The device ID, or 0 on failure.

Example

example.cpp
1SDL_AudioDeviceID dev = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, nullptr);
2SDL_AudioStream* music = SDL_CreateAudioStream(&musicSpec, nullptr);
3SDL_AudioStream* sfx   = SDL_CreateAudioStream(&sfxSpec,   nullptr);
4SDL_BindAudioStream(dev, music);
5SDL_BindAudioStream(dev, sfx);       // both play, mixed
Official SDL wiki