class Player

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/subcl/player.rb, line 5
def initialize
  #TODO add configs for host/port/security
  @mpd = MPD.new
  @mpd.connect
  super(@mpd)
end

Public Instance Methods

add(song, insert = false) click to toggle source

insert: whether to add the song after the currently playing one instead of the end of the queue

# File lib/subcl/player.rb, line 14
def add(song, insert = false)
  unless song[:stream_url]
    raise ArgumentError, "argument has no :stream_url!"
  end
  LOGGER.debug { "Adding #{song['title']}: #{song[:stream_url]}. Insert: #{insert}" }
  if insert then
    pos = @mpd.current_song.pos + 1
    @mpd.addid(song[:stream_url], pos)
  else
    @mpd.add(song[:stream_url])
  end
end
clearstop() click to toggle source

stops the player and clears the playlist

# File lib/subcl/player.rb, line 28
def clearstop
  @mpd.stop
  @mpd.clear
end
pause() click to toggle source
# File lib/subcl/player.rb, line 56
def pause
  @mpd.pause = 1
end
rewind() click to toggle source

if song has been playing for more than 4 seconds, rewind it to the start otherwise go to the previous song

# File lib/subcl/player.rb, line 35
def rewind
  if @mpd.status[:elapsed] > 4
    @mpd.seek 0
  else
    @mpd.previous
  end
end
toggle() click to toggle source

if mpd is playing, pause it. Otherwise resume playback

# File lib/subcl/player.rb, line 44
def toggle
  if @mpd.playing?
    @mpd.pause = 1
  else
    # experimental hack: I think this forces mpd to start downloading the stream again.
    # this should prevent a bug that fails to resume streams after pausing
    # TODO might make this configurable
    @mpd.seek(@mpd.status[:elapsed].to_i)
    @mpd.pause = 0
  end
end