The sound API in DragonRuby is a little tricky because at first it seems similar to the sprite API (e.g. using args.outputs
) but it diverges sharply when you want to have some more control.
So let’s take a quick look at audio in DragonRuby!
Simple Music
As usual with DragonRuby, the simple stuff is simple.
To play a looping sound, for example as background music, get a sound file in .ogg
format and then add it on to args.outputs.sounds
:
1
|
|
The audio will loop forever.
Simple Sound Effects
Want to fire off a single sound effect?
Get a sound file in .wav
format and then add it on to args.outputs.sounds
:
1
|
|
The sound will play once.
Wait so the API behaves differently based on the file format?
Yep. A little odd but it works.
Adjusting the Volume Knob
Playing a sound effect in DragonRuby is pretty easy. But what if we need to adjust the volume or some other attribute of the audio? Or maybe loop a .wav
or not loop a .ogg
file?
You might think, based on how sprites work in DragonRuby, you could do something like this:
1 2 3 4 5 6 |
|
But this does not work.
Instead, you need to use an entirely different interface: args.audio
.
args.audio
is a hash table of sounds.
To add a new sound, assign it with a name:
1 2 3 4 5 |
|
To adjust an attribute of the audio, access it by the same name:
1 2 |
|
To pause a sound:
1
|
|
To completely remove a sound:
1
|
|
More Music Manipulation
This is the full set of options for audio from the documentation:
1 2 3 4 5 6 7 8 |
|
That’s it!