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.



132
133
134
135
# File 'lib/muzak/player/mpd.rb', line 132

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



93
94
95
96
97
98
99
# File 'lib/muzak/player/mpd.rb', line 93

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



105
106
107
108
109
110
111
# File 'lib/muzak/player/mpd.rb', line 105

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



83
84
85
86
87
# File 'lib/muzak/player/mpd.rb', line 83

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



117
118
119
120
121
# File 'lib/muzak/player/mpd.rb', line 117

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



150
151
152
153
154
# File 'lib/muzak/player/mpd.rb', line 150

def load_song(song)
  path = song.path.sub("#{Config.music}/", "")
  @mpd.add(path)
  @mpd.play if Config.autoplay && !playing?
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.



68
69
70
# File 'lib/muzak/player/mpd.rb', line 68

def next_song
  @mpd.next
end

#now_playingSong?

Get MPD's currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song



139
140
141
142
143
144
# File 'lib/muzak/player/mpd.rb', line 139

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.



52
53
54
55
56
# File 'lib/muzak/player/mpd.rb', line 52

def pause
  return unless running?

  @mpd.pause = 1
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
46
47
# File 'lib/muzak/player/mpd.rb', line 43

def play
  return unless running?

  @mpd.play
end

#playing?Boolean

Returns whether or not MPD is currently playing.

Returns:

  • (Boolean)

    whether or not MPD is currently playing.



59
60
61
62
63
# File 'lib/muzak/player/mpd.rb', line 59

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.



75
76
77
# File 'lib/muzak/player/mpd.rb', line 75

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.



126
127
128
# File 'lib/muzak/player/mpd.rb', line 126

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