Class: Gosu::Sample

Inherits:
Object
  • Object
show all
Defined in:
lib/gosu_android/audio/audio.rb

Overview

TODO ManageAudioFocus, when app loses, stop song TODO Raise a warning is the file could not be loaded

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, filename) ⇒ Sample

Constructs a sample that can be played on the specified audio system and loads the sample from a file.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gosu_android/audio/audio.rb', line 57

def initialize(window, filename)
  @window = window
  if not defined? @@pool
    @@pool = JavaImports::SoundPool.new(MAX_SAMPLES, JavaImports::AudioManager::STREAM_MUSIC, 0)
  end

  if(filename.class == Fixnum )
    @id = @@pool.load( @window.activity.getApplicationContext, filename, 1 )
  else
    @id = @@pool.load(filename, 1)
  end
  #Set finalize
  ObjectSpace.define_finalizer(self, self.class.finalize(@id))      
end

Class Method Details

.finalize(id) ⇒ Object



95
96
97
# File 'lib/gosu_android/audio/audio.rb', line 95

def self.finalize(id)
  proc { @@pool.unload(id) }
end

Instance Method Details

#play(volume = 1, speed = 1, looping = false) ⇒ Object

Plays the sample without panning. param volume Can be anything from 0.0 (silence) to 1.0 (full volume). param speed Playback speed is only limited by the underlying audio library, and can accept very high or low values. Use 1.0 for normal playback speed.



78
79
80
81
82
83
84
# File 'lib/gosu_android/audio/audio.rb', line 78

def play(volume = 1, speed = 1, looping = false)
  if looping == false
    @stream_id = @@pool.play(@id, volume, volume, 1, 0, 1.0)
  else
    @stream_id = @@pool.play(@id, volume, volume, 1, 1, 1.0)
  end
end

#play_pan(pan = 0, volume = 1, speed = 1, looping = false) ⇒ Object

TODO Pan is not supported so it is ignored



87
88
89
90
91
92
93
# File 'lib/gosu_android/audio/audio.rb', line 87

def play_pan(pan = 0, volume = 1, speed = 1, looping = false)
  if looping == false
    @stream_id = @@pool.play(@id, volume, volume, 1, 0, 1.0)
  else
    @stream_id = @@pool.play(@id, volume, volume, 1, 1, 1.0)
  end
end