Created
May 6, 2013 02:20
-
-
Save twerth/5523040 to your computer and use it in GitHub Desktop.
Playing system sounds in RubyMotion
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
# Add the following to your Rakefile | |
# app.frameworks += ['MediaPlayer'] | |
class SystemSounds | |
class << self | |
BASE_AUDIO_PATH = "#{NSBundle.mainBundle.resourcePath}/audio/" | |
@@system_sounds = {} | |
def play_system_sound(file_name) | |
path = "#{BASE_AUDIO_PATH}#{file_name}" | |
sound_id = find_or_create_sound_id(path) | |
AudioServicesPlaySystemSound(sound_id[0]) | |
end | |
def find_or_create_sound_id(file_name) | |
unless sound_id = @@system_sounds[file_name] | |
sound_id = Pointer.new('I') | |
@@system_sounds[file_name] = sound_id | |
AudioServicesCreateSystemSoundID(NSURL.fileURLWithPath(file_name), sound_id) | |
end | |
sound_id | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@chrise86 try it on device... Working for me