Search code examples
rubylibgosu

Gosu::Song in Ruby only playing first note


I'm trying to write a simple game in Ruby (ver 2.2.6) on Windows. I installed the Gosu gem to handle the audio, and I have a soundtrack file that I would like to play:

@soundtrack = Gosu::Song.new("theme.ogg")

I cannot use Sample, because I need to be able to pause.

 def play_soundtrack
    @soundtrack.play(looping = true)
  end

  def pause_soundtrack
    print "Paused "
    @soundtrack.pause
  end

Running this only plays the first note of the theme, and then no sound will play. Gosu::Sample still works as it should, so I'm not sure what could be wrong with my use of Gosu::Song.


Solution

  • This is not possible with Gosu right now, Song#play does not work without a window. Because Gosu does not use a background thread to play audio, there is an internal Song::update() method that needs to be called periodically to keep the song playing. Gosu::Window#show does this once per tick. There is no way to directly call this method from Ruby. The only workaround is to show a tiny window while audio is playing.

    I've opened a GitHub issue because this should either be documented or fixed.