class RubyAfplay::Player

Public Class Methods

new(file_path, options = {}) click to toggle source
# File lib/ruby_afplay/player.rb, line 4
def initialize(file_path, options = {})
  @command = file_path + build_options_string(options)
end

Public Instance Methods

pause() click to toggle source
# File lib/ruby_afplay/player.rb, line 17
def pause
  # send a stop signal to afplay using the process_id
  Process.kill("SIGSTOP", @process_id)
end
play() click to toggle source
# File lib/ruby_afplay/player.rb, line 8
def play
  if @process_id
    # resume playing if the process is already running
    Process.kill("SIGCONT", @process_id)
  else
    start_process!
  end
end
stop() click to toggle source
# File lib/ruby_afplay/player.rb, line 22
def stop
  # send a kill signal to aflpay using the process_id
  Process.kill("SIGKILL", @process_id)
  # detach the process
  detach_process!
end

Private Instance Methods

build_options_string(options) click to toggle source
# File lib/ruby_afplay/player.rb, line 31
def build_options_string(options)
  valid_options = %i(volume time rate quality)
  options_string = ""
  options.each do |key, value|
    options_string += " -#{key[0]} #{value}" if valid_options.include?(key)
  end
  options_string
end
detach_process!() click to toggle source
# File lib/ruby_afplay/player.rb, line 47
def detach_process!
  # detaches process
  Process.detach(@process_id)
  # clear the process_id reference
  @process_id = nil?
end
start_process!() click to toggle source
# File lib/ruby_afplay/player.rb, line 40
def start_process!
  # start playing by running 'afplay'
  # spawn lets us returns a process_id we can use with other commands
  @process_id = spawn "afplay #{@command}"
end