Let’s Make a Game 5: Sounds
Shoes doesn’t have great audio support right now, but it ships with multimedia capabilities provided by the VideoLAN and ffmpeg libraries.
To use audio, use the video keyword with a wav or mp3. You can hide the resulting video object so it won’t take up layout space. One limitation of audio playback is you only have a single stereo channel, which means you can’t multiplex audio (I might be wrong, but I haven’t found a way to do this). This is usually required in a game, so our audio will be fairly limited, but good enough to get things going.
I’ve made a small class to manage audio in our snake game. It makes managing sounds easier:
- Add sounds with
@sounds.add_sound :death, 'death.mp3' - Playing a sound will automatically stop other sounds
- I’ve bundled sounds for collecting items and death
The current version of this tutorial in my snake-shoes git repository is here: snake-shoes-6d88. You can download the sounds and code there.
The sound class looks like this:
Other Options for Sound
One way of handling audio would be to rely on a library like libSDL. Rubysql would be a good starting point.
Dependencies
If your Shoes applications start to require libraries, you can manage them with Shoes.setup:
Shoes.setup do gem 'library' end

