class Gosu::Song

TODO Error on playing several songs, bear in mind mediaplayer states TODO Set listener for when the data finished loading asynchronously

add some checks play is reached before that

Attributes

current_song[R]

Public Class Methods

new(window, filename) click to toggle source
# File lib/gosu_android/audio/audio.rb, line 106
def initialize(window, filename)
  @window = window
  if not defined? @@media_player
    @@media_player = JavaImports::MediaPlayer.new
    @@audio_focus_listener = AudioFocusListener.new
    context = @window.activity.getApplicationContext
    @@audio_manager = context.getSystemService(Context::AUDIO_SERVICE)
    focus = @@audio_manager.requestAudioFocus(@@audio_focus_listener, JavaImports::AudioManager::STREAM_MUSIC, JavaImports::AudioManager::AUDIOFOCUS_GAIN)
    @@media_player.setOnCompletionListener AudioCompletionListener.new
    @@media_player.setOnErrorListener AudioErrorListener.new
    @@media_player.setOnInfoListener AudioInfoListener.new
  else
    @@media_player.reset
    focus = @@audio_manager.requestAudioFocus(@@audio_focus_listener, JavaImports::AudioManager::STREAM_MUSIC, JavaImports::AudioManager::AUDIOFOCUS_GAIN)
  end

  if filename.class == Fixnum
    afd = @window.activity.getApplicationContext.getResources.openRawResourceFd(filename)
    filename = afd.getFileDescriptor
  end

  @@media_player.on_prepared_listener = (proc{media_player_ready})
  @@media_player.setDataSource filename
  @@media_player.prepareAsync
  @player_ready = false
  @window.media_player = @@media_player
  @playing = false
  @file_name = filename

  if not defined? @@current_song
    @@current_song = 0
  end
end
release_resources() click to toggle source
# File lib/gosu_android/audio/audio.rb, line 190
def Song.release_resources
  if defined? @@media_player
    @@audio_manager.abandonAudioFocus @@audio_focus_listener
  end
end

Public Instance Methods

media_player_ready() click to toggle source
# File lib/gosu_android/audio/audio.rb, line 140
def media_player_ready
  @player_ready = true
  #Song should be playing but media player was not ready
  #so start playing now
  if @playing
    @@media_player.start
  end
end
pause() click to toggle source

Pauses playback of the song. It is not considered being played. currentSong will stay the same.

# File lib/gosu_android/audio/audio.rb, line 162
def pause
  if @player_ready
    @@media_player.pause
  end
  @playing = false
end
paused?() click to toggle source

Returns true if the song is the current song, but in paused mode.

# File lib/gosu_android/audio/audio.rb, line 171
def paused?
  not @playing and @@current_song == @file_name
end
play(looping = false) click to toggle source

Starts or resumes playback of the song. This will stop all other songs and set the current song to this object.

# File lib/gosu_android/audio/audio.rb, line 151
def play(looping = false)
  @@media_player.setLooping(looping)
  if @player_ready
    @@media_player.start
  end
  @@current_song = @file_name
  @playing = true
end
playing?() click to toggle source

Returns true if the song is currently playing.

# File lib/gosu_android/audio/audio.rb, line 176
def playing?
  @playing and @@current_song == @file_name
end
stop() click to toggle source

Stops playback of this song if it is currently played or paused. Afterwards, current_song will return 0.

# File lib/gosu_android/audio/audio.rb, line 182
def stop
  if @player_ready
    @@media_player.pause
    @@media_player.stop
  end
  @@current_song = 0
end