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.



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_queuevoid

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

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



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

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



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

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



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_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



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.

Parameters:

  • song (Song)

    the song to load



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 && !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.



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

def next_song
  @mpd.next
end

#now_playingSong?

Get MPD's currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song



141
142
143
144
145
146
# 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

#pausevoid

Note:

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

#playvoid

Note:

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.

Returns:

  • (Boolean)

    whether or not MPD is currently playing.



61
62
63
64
65
# File 'lib/muzak/player/mpd.rb', line 61

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.



77
78
79
# File 'lib/muzak/player/mpd.rb', line 77

def previous_song
  @mpd.previous
end

#running?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/muzak/player/mpd.rb', line 9

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

#shufflevoid

TODO:

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