Ruby声音

来源:互联网 发布:火鸟中文编程 编辑:程序博客网 时间:2024/04/29 18:33

    转 Adding Sound to Your Ruby Apps

    Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Perhaps a humorous sound to accompany an error message .

    The win32-sound library makes using sounds really simple.

    If you installed Ruby with the One-Click Ruby Installer, then you probably already have the win32-sound library installed, along with other win32 utilities. Otherwise, you can install it in seconds via gem. Open up a console window and enter..

    gem install win32-sound

    To use the win32-sound library, add these require and include statements to the top of your script...

    require 'win32/sound'

    include Win32

    Then, to play a sound file on the PC, call the Sound.play method, passing it the name of the file...

    Sound.play('chimes.wav')

    Sound.play('c:/sounds/hal9000.wav')

    In the example above, you don't have to include the path to the file 'chimes.wav', because 'chimes.wav' is usually installed in the Windows folder. But in most cases, you'll want to include the full path to the sound file.

    To generate a simple beep, call the Sound.beep method, passing it the tone frequency (in Hertz, between 37 and 32767) and the duration in milliseconds. For example, to play a low tone for half a second...

    Sound.beep(100, 500)

    ...or to play an annoyingly high-pitched tone for 3 full seconds...

    Sound.beep(5000, 3000)

    The complete docs for this library can be found here.

    Distributing your sound files with your application is also simple. You can embed your sound files in an executable created with RubyScript2Exe, or include them in an install package produced with Inno Setup.

    Be careful not to overdo it, though, if your program is to be used by others besides yourself. It's a fine line between clever and annoying.

    注:

    win-32声音库文件夹位置为

    C:/WINDOWS/Media

    Sound.play()可以正常运行,但是好像只能播放wav格式声音,像mp3和mdi都是“咚”的一声,不能播放。

    Sound.beep()没有声音,代码也不会报错。

    查找网上说在注册表中可以配置beep是否发声:

    Windows出错时发出的警告声令人十分不悦,关掉它好了。打开HKEY_CURRENT_USER/Con trol Panel/Sound,将Beep 改为"No" 就不能发声,改为"Yes"则相反。

    但是我的该项注册表值就是YES,如下图

    clip_image001

    这个问题很困扰,不知道什么原因...