[SDL] SDL_Sound simple exemple
Ryan C. Gordon
icculus at clutteredmind.org
Fri Sep 26 00:14:01 PDT 2003
> finally i get the sdl_sound running....
> i took a look at the playsound.c but this exemple is complicated (i'm
> learning C++)
> i only want play an mp3 sample, can someone has an easy exemple to show
> me how to do this
If you were to cut out all the extras from playsound, it'd look roughly
static volatile int still_playing_audio = 1;
sample = Sound_NewSampleFromFile("mysound.mp3", NULL, 16384);
memcpy(&devspec, &sample->actual, sizeof (SDL_AudioSpec));
devspec.callback = my_audio_callback;
devspec.samples = 4096;
devspec.userdata = sample;
still_playing_audio = 1;
SDL_Delay(10); // sleep awhile.
- Cast the user-defined void * to a Sound_Sample *.
- See if there's data left in sample->buffer.
- If not, call Sound_Decode().
- memcpy() from appropriate place in sample->buffer to audio stream.
- update state variables (where in sample->buffer you are, etc).
- If sample->flags indicate error or eof and we've exhausted
sample->buffer, set still_playing_audio to zero.
If you just want to play an MP3, consider SDL_mixer instead.
More information about the SDL