Class: Muzak::Player::MPD

Inherits:
StubPlayer show all
Defined in:
lib/muzak/player/mpd.rb

Overview

Exposes a MPD connection to muzak for playback control.

Instance Attribute Summary

Attributes inherited from StubPlayer

#instance

Instance Method Summary collapse

Methods inherited from StubPlayer

available?, #initialize, player_name, #shuffle_queue

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Constructor Details

This class inherits a constructor from Muzak::Player::StubPlayer

Instance Method Details

#activate!void

This method returns an undefined value.

Activates the MPD connection.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/muzak/player/mpd.rb', line 13

def activate!
  return if running?

  debug "activating #{self.class}"

  host = Config.mpd_host || "localhost"
  port = Config.mpd_port || 6600

  @mpd = ::MPD.new host, port
  @mpd.connect
  @mpd.clear

  instance.event :player_activated
end

#clear_queuevoid

This method returns an undefined value.

Clear MPD's internal queue.



128
129
130
131
# File 'lib/muzak/player/mpd.rb', line 128

def clear_queue
  return unless running?
  @mpd.clear
end

#deactivate!void

This method returns an undefined value.

Deactivates the MPD connection.



30
31
32
33
34
35
36
37
38
# File 'lib/muzak/player/mpd.rb', line 30

def deactivate!
  @mpd.clear
  sleep 0.1 # give mpd a little bit of time to process
  @mpd.disconnect

  debug "deactivating #{self.class}"

  instance.event :player_deactivated
end

#enqueue_album(album) ⇒ void

Note:

Activates MPD if not already activated.

This method returns an undefined value.

Tell MPD to add the given album to its queue.

Parameters:

  • album (Album)

    the album to add



89
90
91
92
93
94
95
# File 'lib/muzak/player/mpd.rb', line 89

def enqueue_album(album)
  activate! unless running?

  album.songs.each do |song|
    load_song song
  end
end

#enqueue_playlist(playlist) ⇒ void

Note:

Activates MPD if not already activated.

This method returns an undefined value.

Tell MPD to add the given playlist to its queue.

Parameters:

  • playlist (Playlist)

    the playlist to add



101
102
103
104
105
106
107
# File 'lib/muzak/player/mpd.rb', line 101

def enqueue_playlist(playlist)
  activate! unless running?

  playlist.songs.each do |song|
    load_song song
  end
end

#enqueue_song(song) ⇒ void

Note:

Activates MPD if not already activated.

This method returns an undefined value.

Tell MPD to add the given song to its queue.

Parameters:

  • song (Song)

    the song to add



79
80
81
82
83
# File 'lib/muzak/player/mpd.rb', line 79

def enqueue_song(song)
  activate! unless running?

  load_song song
end

#list_queueArray<Song>

TODO:

Implement this.

Note:

This includes songs already played.

Get MPD's internal queue.

Returns:

  • (Array<Song>)

    all songs in MPD's queue



113
114
115
116
117
# File 'lib/muzak/player/mpd.rb', line 113

def list_queue
  debug @mpd.playlist.to_s
  danger "this player doesn't support list_queue"
  []
end

#load_song(song) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Load a song into MPD.

Parameters:

  • song (Song)

    the song to load



146
147
148
149
150
# File 'lib/muzak/player/mpd.rb', line 146

def load_song(song)
  path = song.path.sub("#{Config.music}/", "")
  @mpd.add(path)
  @mpd.play if Config.autoplay
end

#next_songvoid

Note:

Does nothing if the current song is the last.

This method returns an undefined value.

Tell MPD to play the next song in its queue.



64
65
66
# File 'lib/muzak/player/mpd.rb', line 64

def next_song
  @mpd.next
end

#now_playingSong?

Get MPD's currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song



135
136
137
138
139
140
# File 'lib/muzak/player/mpd.rb', line 135

def now_playing
  return unless playing?

  path = "#{Config.music}/#{@mpd.current_song.file}"
  Song.new(path)
end

#pausevoid

Note:

Does nothing is playback is already paused.

This method returns an undefined value.

Tell MPD to pause playback.



50
51
52
# File 'lib/muzak/player/mpd.rb', line 50

def pause
  @mpd.pause
end

#playvoid

Note:

Does nothing is playback is already in progress.

This method returns an undefined value.

Tell MPD to begin playback.



43
44
45
# File 'lib/muzak/player/mpd.rb', line 43

def play
  @mpd.play
end

#playing?Boolean

Returns whether or not MPD is currently playing.

Returns:

  • (Boolean)

    whether or not MPD is currently playing.



55
56
57
58
59
# File 'lib/muzak/player/mpd.rb', line 55

def playing?
  return false unless running?

  @mpd.playing?
end

#previous_songvoid

Note:

Restarts the song if the current song is the first.

This method returns an undefined value.

Tell MPD to play the previous song in its queue.



71
72
73
# File 'lib/muzak/player/mpd.rb', line 71

def previous_song
  @mpd.previous
end

#running?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/muzak/player/mpd.rb', line 7

def running?
  !!@mpd&.connected?
end

#shufflevoid

TODO:

Implement this.

This method returns an undefined value.

Shuffle MPD's internal queue.



122
123
124
# File 'lib/muzak/player/mpd.rb', line 122

def shuffle
  danger "this player doesn't support shuffling (?)"
end