class Muzak::Player::MPD

Exposes a MPD connection to muzak for playback control.

Public Instance Methods

activate!() click to toggle source

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_queue() click to toggle source

Clear MPD's internal queue. @return [void]

# File lib/muzak/player/mpd.rb, line 134
def clear_queue
  return unless running?
  @mpd.clear
end
deactivate!() click to toggle source

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
enqueue_album(album) click to toggle source

Tell MPD to add the given album to its queue. @param album [Album] the album to add @return [void] @note Activates MPD if not already activated.

# File lib/muzak/player/mpd.rb, line 95
def enqueue_album(album)
  activate! unless running?

  album.songs.each do |song|
    load_song song
  end
end
enqueue_playlist(playlist) click to toggle source

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
enqueue_song(song) click to toggle source

Tell MPD to add the given song to its queue. @param song [Song] the song to add @return [void] @note Activates MPD if not already activated.

# File lib/muzak/player/mpd.rb, line 85
def enqueue_song(song)
  activate! unless running?

  load_song song
end
list_queue() click to toggle source

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_song(song) click to toggle source

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
next_song() click to toggle source

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
now_playing() click to toggle source

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
pause() click to toggle source

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
play() click to toggle source

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
playing?() click to toggle source

@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
previous_song() click to toggle source

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
running?() click to toggle source
# File lib/muzak/player/mpd.rb, line 9
def running?
  !!@mpd&.connected?
end
shuffle() click to toggle source

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