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 FIXME Set listener for when the data finished loading asynchronously add some checks play is reached before that

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, filename) ⇒ Song

Returns a new instance of Song.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gosu_android/audio/audio.rb', line 76

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)
  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.



75
76
77
# File 'lib/gosu_android/audio/audio.rb', line 75

def current_song
  @current_song
end

Instance Method Details

#media_player_readyObject



107
108
109
110
111
112
113
114
# File 'lib/gosu_android/audio/audio.rb', line 107

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.



129
130
131
132
133
134
# File 'lib/gosu_android/audio/audio.rb', line 129

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)


138
139
140
# File 'lib/gosu_android/audio/audio.rb', line 138

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.



118
119
120
121
122
123
124
125
# File 'lib/gosu_android/audio/audio.rb', line 118

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)


143
144
145
# File 'lib/gosu_android/audio/audio.rb', line 143

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.



149
150
151
152
153
154
155
# File 'lib/gosu_android/audio/audio.rb', line 149

def stop
  if @player_ready
    @@media_player.pause
    @@media_player.stop
  end
  @@current_song = 0
end