module Goby::Music

Methods for playing/stopping background music (BGM).

Public Instance Methods

play_music(filename) click to toggle source

Starts playing the music from the specified file. This has only been tested on Ubuntu w/ .mid files.

@param [String] filename the file containing the music.

# File lib/goby/music.rb, line 26
def play_music(filename)
  return unless @@playback

  if (filename != @@file)
    stop_music
    @@file = filename

    # This thread loops the music until one calls #stop_music.
    @@thread = Thread.new {
      while (true)
        Process.wait(@@pid) if @@pid
        @@pid = Process.spawn("#{@@program} #{filename}", :out=>"/dev/null")
      end
    }
  end
end
set_playback(flag) click to toggle source

Specify if music should play or not. May be useful to stop/start music for dramatic effect.

@param [Boolean] flag true iff music should play.

# File lib/goby/music.rb, line 18
def set_playback(flag)
  @@playback = flag
end
set_program(name) click to toggle source

Specify the program that should play the music. Without overwriting, it is set to a default (see @@program).

@param [String] name the name of the music-playing program.

# File lib/goby/music.rb, line 10
def set_program(name)
  @@program = name
end
stop_music() click to toggle source

Kills the music process and the looping thread.

# File lib/goby/music.rb, line 44
def stop_music
  return unless @@playback

  Process.kill("SIGKILL", @@pid) if @@pid
  @@pid = nil

  @@thread.kill if @@thread
  @@thread = nil

  @@file = nil
end