Skip to content

Instantly share code, notes, and snippets.

@twerth
Created May 6, 2013 02:20
Show Gist options
  • Save twerth/5523040 to your computer and use it in GitHub Desktop.
Save twerth/5523040 to your computer and use it in GitHub Desktop.
Playing system sounds in RubyMotion
# 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
@zxiest
Copy link

zxiest commented Jun 24, 2015

@chrise86 try it on device... Working for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment