class DXOpal::Sound
Attributes
decoded[RW]
Public Class Methods
_load(path_or_url)
click to toggle source
Load remote sound (called via Window.load_resources
)
# File lib/dxopal/sound.rb, line 15 def self._load(path_or_url) snd = new(path_or_url) snd_promise = %x{ new Promise(function(resolve, reject) { var request = new XMLHttpRequest(); request.open('GET', #{path_or_url}, true); request.responseType = 'arraybuffer'; request.onload = function() { var audioData = request.response; var context = #{Sound.audio_context}; context.decodeAudioData(audioData, function(decoded) { snd['$decoded='](decoded); resolve(); }); }; request.send(); }); } return snd, snd_promise end
audio_context()
click to toggle source
Return AudioContext
# File lib/dxopal/sound.rb, line 8 def self.audio_context @@audio_context ||= %x{ new (window.AudioContext||window.webkitAudioContext) } end
new(path_or_url)
click to toggle source
# File lib/dxopal/sound.rb, line 36 def initialize(path_or_url) @path_or_url = path_or_url # Used in error message end
Public Instance Methods
play()
click to toggle source
Play this sound once
# File lib/dxopal/sound.rb, line 42 def play raise "Sound #{path_or_url} is not loaded yet" unless @decoded source = nil %x{ var context = #{Sound.audio_context}; source = context.createBufferSource(); source.buffer = #{@decoded}; source.connect(context.destination); source.start(0); } @source = source end
stop()
click to toggle source
Stop playing this sound (if playing)
# File lib/dxopal/sound.rb, line 56 def stop return unless @decoded return unless @source @source.JS.stop() end