Class: Gosu::Song

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, filename) ⇒ Song

Returns a new instance of Song.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

Instance Attribute Details

#current_songObject (readonly)

Returns the value of attribute current_song.



105
106
107
# File 'lib/gosu_android/audio/audio.rb', line 105

def current_song
  @current_song
end

Class Method Details

.release_resourcesObject



190
191
192
193
194
# 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

Instance Method Details

#media_player_readyObject



140
141
142
143
144
145
146
147
# 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

#pauseObject

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



162
163
164
165
166
167
# File 'lib/gosu_android/audio/audio.rb', line 162

def pause
  if @player_ready
    @@media_player.pause
  end
  @playing = false
end

#paused?Boolean

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

Returns:

  • (Boolean)


171
172
173
# File 'lib/gosu_android/audio/audio.rb', line 171

def paused?
  not @playing and @@current_song == @file_name
end

#play(looping = false) ⇒ Object

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



151
152
153
154
155
156
157
158
# 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?Boolean

Returns true if the song is currently playing.

Returns:

  • (Boolean)


176
177
178
# File 'lib/gosu_android/audio/audio.rb', line 176

def playing?
  @playing and @@current_song == @file_name
end

#stopObject

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



182
183
184
185
186
187
188
# 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