class Muzak::Player::MPD
Exposes a MPD
connection to muzak for playback control.
Public Instance Methods
Activates the MPD
connection. @return [void]
# File lib/muzak/player/mpd.rb, line 15 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 MPD's internal queue. @return [void]
# File lib/muzak/player/mpd.rb, line 134 def clear_queue return unless running? @mpd.clear end
Deactivates the MPD
connection. @return [void]
# File lib/muzak/player/mpd.rb, line 32 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
Tell MPD
to add the given playlist to its queue. @param playlist [Playlist] the playlist to add @return [void] @note Activates MPD
if not already activated.
# File lib/muzak/player/mpd.rb, line 107 def enqueue_playlist(playlist) activate! unless running? playlist.songs.each do |song| load_song song end end
Get MPD's internal queue. @return [Array<Song>] all songs in MPD's queue @note This includes songs already played. @todo Implement this.
# File lib/muzak/player/mpd.rb, line 119 def list_queue debug @mpd.playlist.to_s danger "this player doesn't support list_queue" [] end
Load a song into MPD
. @param song [Song] the song to load @return [void] @api private
# File lib/muzak/player/mpd.rb, line 152 def load_song(song) path = song.path.sub("#{Config.music}/", "") @mpd.add(path) @mpd.play if Config.autoplay && !playing? end
Tell MPD
to play the next song in its queue. @return [void] @note Does nothing if the current song is the last.
# File lib/muzak/player/mpd.rb, line 70 def next_song @mpd.next end
Get MPD's currently loaded song. @return [Song, nil] the currently loaded song
# File lib/muzak/player/mpd.rb, line 141 def now_playing return unless playing? path = "#{Config.music}/#{@mpd.current_song.file}" Song.new(path) end
Tell MPD
to pause playback. @return [void] @note Does nothing is playback is already paused.
# File lib/muzak/player/mpd.rb, line 54 def pause return unless running? @mpd.pause = 1 end
Tell MPD
to begin playback. @return [void] @note Does nothing is playback is already in progress.
# File lib/muzak/player/mpd.rb, line 45 def play return unless running? @mpd.play end
@return [Boolean] whether or not MPD
is currently playing.
# File lib/muzak/player/mpd.rb, line 61 def playing? return false unless running? @mpd.playing? end
Tell MPD
to play the previous song in its queue. @return [void] @note Restarts the song if the current song is the first.
# File lib/muzak/player/mpd.rb, line 77 def previous_song @mpd.previous end
# File lib/muzak/player/mpd.rb, line 9 def running? !!@mpd&.connected? end
Shuffle MPD's internal queue. @return [void] @todo Implement this.
# File lib/muzak/player/mpd.rb, line 128 def shuffle danger "this player doesn't support shuffling (?)" end