I’m trying to play a simple WAV file using SDL2. Most tutorials seem to opt to use SDL_mixer, but I instead took the masochistic approach of trying to use SDL2’s native functionality for Audio.
I put together bits and pieces from the documentation and came up with this:
#include <SDL.h>
int main(int argc, char ** argv)
{
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec wav_spec;
Uint32 wav_length;
Uint8 *wav_buffer;
SDL_AudioSpec desired;
SDL_AudioSpec obtained;
SDL_zero(desired);
desired.freq = 48000;
desired.format = AUDIO_F32;
desired.channels = 2;
desired.samples = 4096;
desired.callback = NULL;
if (SDL_LoadWAV("Powerup5.wav", &wav_spec, &wav_buffer, &wav_length))
{
SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
if (deviceId)
{
//SDL_PauseAudioDevice(deviceId, 0);
int success = SDL_QueueAudio(deviceId, wav_buffer, wav_length);
if (success < 0)
SDL_ShowSimpleMessageBox(0, "Error", "Failed to queue audio", NULL);
SDL_Delay(5000);
SDL_CloseAudioDevice(deviceId);
}
else
SDL_ShowSimpleMessageBox(0, "Error", "Audio driver failed to initialize", NULL);
}
else
SDL_ShowSimpleMessageBox(0, "Error", "wav failed to load", NULL);
SDL_Quit();
return 0;
}
Everything is loading fine (I am not getting any message boxes) but no sound is being played. I tried switching the desired AudioSpec with that read from the WAV file, but it didn’t change anything.
Any idea what I’m doing wrong?
