Created
March 20, 2011 10:24
-
-
Save crux/878253 to your computer and use it in GitHub Desktop.
playing apple audio samples in in macruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rvm macruby | |
# code taken from: http://merbist.com/tag/nssound/ | |
framework 'Cocoa' | |
# Cocoa documentation reference: | |
# http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html | |
def sound_dir | |
#@sound_dir ||= "/Library/Audio/Apple Loops/Apple/iLife Sound Effects/Machines/*aif" | |
@sound_dir ||= "/System/Library/Sounds/*.aiff" | |
end | |
def play_sound | |
@sounds.empty? and NSApplication.sharedApplication.terminate(nil) | |
sound_file = @sounds.shift | |
s = NSSound.alloc.initWithContentsOfFile(sound_file, byReference: false) | |
puts "playing: #{sound_file}" | |
s.delegate = self | |
s.play | |
end | |
# This is a delegate method called by the sound object | |
def sound(sound, didFinishPlaying: state) | |
play_sound if state | |
end | |
# Delegate method called when the app finished loading | |
def applicationDidFinishLaunching(notification) | |
@sounds = Dir.glob(sound_dir) #"/System/Library/Sounds/*.aiff") | |
play_sound | |
end | |
# We are delegating the application to self so the script will know when | |
# it finished loading | |
NSApplication.sharedApplication.delegate = self | |
NSApplication.sharedApplication.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment