Sha256: 0e5ab9e06dc952af5fedd9ad5e46901ee9a6793c286094bacd8782218185485d

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 KB

Contents

require 'java'

class PlayAudio
  import "sun.audio.AudioStream"
  import "sun.audio.AudioDataStream"
  import "sun.audio.AudioPlayer"
  import "sun.audio.ContinuousAudioDataStream"
  
  def self.play filename
    i = java.io.FileInputStream.new(filename)
    a = AudioStream.new(i)
    AudioPlayer.player.start(a)
    a
  end
  
  def self.loop filename
    i = java.io.FileInputStream.new(filename)
    a = AudioStream.new(i)
    b = a.get_data # failing means too big of data...
    c = ContinuousAudioDataStream.new(b)
    AudioPlayer.player.start(c)
    c
  end
  
  def initialize filename
    @filename = filename
  end
  
  def start
    raise if @audio_stream
    @audio_stream = PlayAudio.play @filename
  end
  
  def loop # will fail is stream > 1 MB
    raise if @audio_stream
    @audio_stream = PlayAudio.loop @filename
  end
  
  def stop
    raise unless @audio_stream
    AudioPlayer.player.stop(@audio_stream)
    @audio_stream = nil
  end
    
end

if $0 == __FILE__ # unit tests :)
 puts 'syntax: filename.wav'
 a = PlayAudio.new ARGV[0]
 a.start
 sleep 0.1
 a.stop

 a = PlayAudio.new ARGV[0]
 a.loop
 sleep 10
 a.stop
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sensible-cinema-0.28.0 lib/play_audio.rb
sensible-cinema-0.27.0 lib/play_audio.rb
sensible-cinema-0.26.3 lib/play_audio.rb
sensible-cinema-0.26.2 lib/play_audio.rb
sensible-cinema-0.26.1 lib/play_audio.rb
sensible-cinema-0.26.0 lib/play_audio.rb
sensible-cinema-0.25.4 lib/play_audio.rb
sensible-cinema-0.25.3 lib/play_audio.rb
sensible-cinema-0.25.2 lib/play_audio.rb
sensible-cinema-0.25.1 lib/play_audio.rb
sensible-cinema-0.25.0 lib/play_audio.rb
sensible-cinema-0.24.9 lib/play_audio.rb
sensible-cinema-0.24.8 lib/play_audio.rb
sensible-cinema-0.24.7 lib/play_audio.rb
sensible-cinema-0.24.6 lib/play_audio.rb
sensible-cinema-0.24.5 lib/play_audio.rb
sensible-cinema-0.24.4 lib/play_audio.rb
sensible-cinema-0.24.3 lib/play_audio.rb
sensible-cinema-0.24.2 lib/play_audio.rb