Skip to content

Instantly share code, notes, and snippets.

@twerth
Created May 6, 2013 02:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@chrise86
Copy link

I can't seem to get anything to play. I've tried with WAV and AIF. No errors, just no sound. Any ideas?

@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