class Dare::Sound

wrapper for JS Audio takes a path/uri as an argument e.g. song = Dare::Sound.new(‘my_song.mp3’) song.play will then play the mp3 in the browser

Public Class Methods

new(path, opts = {}) click to toggle source

loads an audio resource from a path Sound.new(‘www.google.com/song.mp3’) Sound.new(‘local_song_in_same_directory_of_app_js_file.mp3’) a predefined volume may be passed as an option Sound.new(‘file.mp3’, volume: 0.5)

# File lib/dare/sound.rb, line 16
def initialize(path, opts = {})
  opts[:overlap] ||= 1
  opts[:volume] ||= 1
  opts[:overlap] = 1 if opts[:overlap].to_i < 1
  opts[:overlap] = 10 if opts[:overlap].to_i > 10
  @overlap = opts[:overlap]
  @sounds = []
  @overlap.times do
    `var snd = new Audio(#{path})`
    `snd.volume = #{opts[:volume]}`
    @sounds << `snd`
  end
  @sound = 0
end

Public Instance Methods

pause() click to toggle source

pause the audio resource, halting playback

# File lib/dare/sound.rb, line 54
def pause
  `#{@sound}.pause()`
end
play() click to toggle source

play the audio resource in the window/tab it was created in if resource was paused, it will start playing from where it left off

# File lib/dare/sound.rb, line 46
def play
  @sound += 1
  @sound %= @overlap
  `#{@sounds[@sound]}.play()`
end
time=(time) click to toggle source

seek to particular time in playback. Time passed is in seconds.

# File lib/dare/sound.rb, line 60
def time=(time)
  `#{@sound}.currentTime = #{time}`
end
volume() click to toggle source

retrieve the current volume of the audio resource

# File lib/dare/sound.rb, line 39
def volume
  `#{@sound}.volume`
end
volume=(vol) click to toggle source

set the volume of the audio resource to value between 0 and 1

# File lib/dare/sound.rb, line 33
def volume=(vol)
  `#{@sound}.volume = #{vol}`
end