Hi all, I’m learning SDL and I have a problem while testing the SDL_audio which load .wav code. When I built this code, it couldn’t play any sound. I checked the forum and I found something about hardware and id but I don’t really understand. Can anyone help me ? Thank you for reading. (I’m using SDL 2.0.12 and Code::Blocks 17.12)
#include <iostream>
#include <SDL.h>
#include <SDL_audio.h>
#define FILE_PATH "BabyElephantWalk60.wav"
struct AudioData
{
Uint8* pos;
Uint32 length;
};
void MyAudioCallBack(void* userdata, Uint8* stream, int streamLength)
{
AudioData* audio = (AudioData*)userdata;
if (audio -> length == 0)
{
return;
}
Uint32 length = (Uint32)streamLength;
length = (length > audio->length ? audio->length : length);
SDL_memcmp(stream, audio -> pos, length);
audio -> pos += length;
audio -> length -= length;
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return 1;
SDL_AudioSpec wavSpec;
Uint8* wavStart;
Uint32 wavLength;
if(SDL_LoadWAV(FILE_PATH, &wavSpec, &wavStart, &wavLength) == NULL)
{
//TODO: Proper error handling
std::cerr<<"Error: "<<FILE_PATH
<<" Couldn't load audio file"<<std::endl;
return 1;
}
AudioData audio;
audio.pos = wavStart;
audio.lengt**strong text**h = wavLength;
wavSpec.callback= MyAudioCallBack;
wavSpec.userdata = &audio;
SDL_AudioDeviceID device = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL,
SDL_AUDIO_ALLOW_ANY_CHANGE);
if (device == 0)
{
std::cerr<<"Error: "<<SDL_GetError()<<std::endl;
return 1;
}
SDL_PauseAudioDevice(device, 0);
while(audio.length > 0)
{
SDL_Delay(100);
}
SDL_CloseAudioDevice(device);
SDL_FreeWAV(wavStart);
SDL_Quit();
return 0;
}