-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwav_mix.c
46 lines (36 loc) · 1016 Bytes
/
wav_mix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#define WAV_PATH "PATH/TO/AUDIO.wav"
// Play a wav audio file using SDL_mixer
int main(int argc, char* argv[]){
printf("Playing %s\n", WAV_PATH);
// Init SDL
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
printf("SDL init error: %s\n", SDL_GetError());
return 1;
}
// Init the mixer api
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
printf("SDL mix error: %s\n", Mix_GetError());
return 1;
}
// Load the wav
Mix_Music *audio = Mix_LoadMUS(WAV_PATH);
if (!audio) {
printf("SDL mix error: %s\n", Mix_GetError());
return 1;
}
// Start playing audio
if (Mix_PlayMusic(audio, 1) < 0) {
printf("Mix_PlayMusic error: %s\n", Mix_GetError());
return 1;
}
// Wait for the audio to finish
while (Mix_PlayingMusic())
SDL_Delay(100);
// Clean up
Mix_FreeMusic(audio);
Mix_Quit();
SDL_Quit();
}