[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
like this:
global:
static volatile int still_playing_audio = 1;
in main:
SDL_Init(SDL_INIT_AUDIO);
Sound_Init();
Sound_Sample *sample;
sample = Sound_NewSampleFromFile("mysound.mp3", NULL, 16384);
SDL_AudioSpec devspec;
memcpy(&devspec, &sample->actual, sizeof (SDL_AudioSpec));
devspec.callback = my_audio_callback;
devspec.samples = 4096;
devspec.userdata = sample;
Sound_Decode(sample);
still_playing_audio = 1;
SDL_OpenAudio(&devspec, NULL);
SDL_PauseAudio(0);
while (still_playing_audio)
SDL_Delay(10); // sleep awhile.
SDL_Quit();
Sound_FreeSample(sample);
return(0);
in my_audio_callback:
- 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.
- return.
If you just want to play an MP3, consider SDL_mixer instead.
--ryan.
More information about the SDL
mailing list