Class: Player

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/subcl/player.rb

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



5
6
7
8
9
10
# File 'lib/subcl/player.rb', line 5

def initialize
  #TODO add configs for host/port/security
  @mpd = MPD.new
  @mpd.connect
  super(@mpd)
end

Instance Method Details

#add(song, insert = false) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
24
25
# 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

#clearstopObject

stops the player and clears the playlist



28
29
30
31
# File 'lib/subcl/player.rb', line 28

def clearstop
  @mpd.stop
  @mpd.clear
end

#pauseObject



56
57
58
# File 'lib/subcl/player.rb', line 56

def pause
  @mpd.pause = 1
end

#rewindObject

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



35
36
37
38
39
40
41
# File 'lib/subcl/player.rb', line 35

def rewind
  if @mpd.status[:elapsed] > 4
    @mpd.seek 0
  else
    @mpd.previous
  end
end

#toggleObject

if mpd is playing, pause it. Otherwise resume playback



44
45
46
47
48
49
50
51
52
53
54
# 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