Skip to content

Tutorial 05 Playing sound

Oliver Simmons edited this page Feb 5, 2022 · 1 revision

Tutorial 05 - Playing sound

Playing sound is very easy with the SDL_mixer API.

Download full example: 05-sound.tgz

Loading SDL_mixer

Just like other additional modules, SDL_mixer must be loaded separately.

local mixer = require("SDL.mixer")

Initialize SDL_mixer

Open the audio device with standard defaults.

assert(mixer.openAudio(44100, SDL.audioFormat.S16, 2, 1024))

Load the sound

Now we can load a chunk and play it to the first channel.

local sound = assert(mixer.loadWAV("gun.wav"))

Play the sound

Play the sound on the first channel.

sound:playChannel(1)
-- Wait for the sound to play
SDL.delay(5000)
Clone this wiki locally