Class: Muzak::Player::MPD
- Inherits:
-
StubPlayer
- Object
- StubPlayer
- Muzak::Player::MPD
- 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 Method Summary collapse
-
#activate! ⇒ void
Activates the MPD connection.
-
#clear_queue ⇒ void
Clear MPD's internal queue.
-
#deactivate! ⇒ void
Deactivates the MPD connection.
-
#enqueue_album(album) ⇒ void
Tell MPD to add the given album to its queue.
-
#enqueue_playlist(playlist) ⇒ void
Tell MPD to add the given playlist to its queue.
-
#enqueue_song(song) ⇒ void
Tell MPD to add the given song to its queue.
-
#list_queue ⇒ Array<Song>
Get MPD's internal queue.
-
#load_song(song) ⇒ void
private
Load a song into MPD.
-
#next_song ⇒ void
Tell MPD to play the next song in its queue.
-
#now_playing ⇒ Song?
Get MPD's currently loaded song.
-
#pause ⇒ void
Tell MPD to pause playback.
-
#play ⇒ void
Tell MPD to begin playback.
-
#playing? ⇒ Boolean
Whether or not MPD is currently playing.
-
#previous_song ⇒ void
Tell MPD to play the previous song in its queue.
- #running? ⇒ Boolean
-
#shuffle ⇒ void
Shuffle MPD's internal queue.
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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 ⇒ void
This method returns an undefined value.
Clear MPD's internal queue.
134 135 136 137 |
# File 'lib/muzak/player/mpd.rb', line 134 def clear_queue return unless running? @mpd.clear end |
#deactivate! ⇒ void
This method returns an undefined value.
Deactivates the MPD connection.
32 33 34 35 36 37 38 39 40 |
# 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) ⇒ void
Activates MPD if not already activated.
This method returns an undefined value.
Tell MPD to add the given album to its queue.
95 96 97 98 99 100 101 |
# 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) ⇒ void
Activates MPD if not already activated.
This method returns an undefined value.
Tell MPD to add the given playlist to its queue.
107 108 109 110 111 112 113 |
# 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) ⇒ void
Activates MPD if not already activated.
This method returns an undefined value.
Tell MPD to add the given song to its queue.
85 86 87 88 89 |
# File 'lib/muzak/player/mpd.rb', line 85 def enqueue_song(song) activate! unless running? load_song song end |
#list_queue ⇒ Array<Song>
Implement this.
This includes songs already played.
Get MPD's internal queue.
119 120 121 122 123 |
# 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) ⇒ 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.
152 153 154 155 156 |
# 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 && ! end |
#next_song ⇒ void
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.
70 71 72 |
# File 'lib/muzak/player/mpd.rb', line 70 def next_song @mpd.next end |
#now_playing ⇒ Song?
Get MPD's currently loaded song.
141 142 143 144 145 146 |
# File 'lib/muzak/player/mpd.rb', line 141 def return unless path = "#{Config.music}/#{@mpd.current_song.file}" Song.new(path) end |
#pause ⇒ void
Does nothing is playback is already paused.
This method returns an undefined value.
Tell MPD to pause playback.
54 55 56 57 58 |
# File 'lib/muzak/player/mpd.rb', line 54 def pause return unless running? @mpd.pause = 1 end |
#play ⇒ void
Does nothing is playback is already in progress.
This method returns an undefined value.
Tell MPD to begin playback.
45 46 47 48 49 |
# File 'lib/muzak/player/mpd.rb', line 45 def play return unless running? @mpd.play end |
#playing? ⇒ Boolean
Returns whether or not MPD is currently playing.
61 62 63 64 65 |
# File 'lib/muzak/player/mpd.rb', line 61 def return false unless running? @mpd. end |
#previous_song ⇒ void
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.
77 78 79 |
# File 'lib/muzak/player/mpd.rb', line 77 def previous_song @mpd.previous end |
#running? ⇒ Boolean
9 10 11 |
# File 'lib/muzak/player/mpd.rb', line 9 def running? !!@mpd&.connected? end |
#shuffle ⇒ void
Implement this.
This method returns an undefined value.
Shuffle MPD's internal queue.
128 129 130 |
# File 'lib/muzak/player/mpd.rb', line 128 def shuffle danger "this player doesn't support shuffling (?)" end |