X68000

Playing soundfiles using Ruby (Windows)

by admin on Jul.15, 2009, under Programming

If you ever wanted to play soundfiles using Ruby on a windows system, then there is a nice library available which can help you with this task. It is called win32-sound and makes use of the Windows Multimedia API. You already have the library when you used the one-click-installer, and it can be found in ..ruby\gems\1.8\gems\win32-sound-0.4.1 (this is the latest version right now, you might have another one).

Setup

If you dont have the library yet, it can be installed from the command line using:

gem install win32-sound

Once installed and ready, we can use the library in our projects. At first, it must be included by adding these lines to your project:

require ‘win32/sound’
include Win32

After doing so, various methods are available. Here are some examples:

Examples

To play a sound file, all you need is the following code:

Sound.play(‘chimes.wav’)

To loop the sound for 3 seconds, you can use this:

Sound.play(‘chimes.wav’,Sound::ASYNC|Sound::LOOP)
sleep 3
Sound.stop

For more examples, take a look at the sound_test.rb, which comes with the library, or see the documentation over at this link.


Leave a Reply

x68000.de

yay

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...