Class: Muzak::Player::MultiPlayer

Inherits:
StubPlayer show all
Defined in:
lib/muzak/player/multiplayer.rb

Overview

Wraps multiple players into a single class. This can be useful for controlling a local and remote player simultaneously, e.g. one MPD and one MPV via the same Muzak instance.

Instance Attribute Summary collapse

Attributes inherited from StubPlayer

#instance

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StubPlayer

player_name

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Constructor Details

#initialize(instance) ⇒ MultiPlayer

Returns a new instance of MultiPlayer.

Parameters:

  • instance (Instance)

    the instance associated with the player


19
20
21
22
23
24
# File 'lib/muzak/player/multiplayer.rb', line 19

def initialize(instance)
  super(instance)

  klasses = Config.multiplayer_players.map { |p| Player::PLAYER_MAP[p] }
  @players = klasses.map { |pk| pk.new(instance) }
end

Instance Attribute Details

#playersArray<StubPlayer> (readonly)

Returns the players associated with this multiplayer.

Returns:

  • (Array<StubPlayer>)

    the players associated with this multiplayer


10
11
12
# File 'lib/muzak/player/multiplayer.rb', line 10

def players
  @players
end

Class Method Details

.available?Boolean

Returns whether or not all of the players are available.

Returns:

  • (Boolean)

    whether or not all of the players are available


13
14
15
16
# File 'lib/muzak/player/multiplayer.rb', line 13

def self.available?
  klasses = Config.multiplayer_players.map { |p| Player::PLAYER_MAP[p] }
  klasses.all?(&:available?)
end

Instance Method Details

#activate!void

This method returns an undefined value.

Activates each player under the multiplayer.


33
34
35
36
# File 'lib/muzak/player/multiplayer.rb', line 33

def activate!
  debug "activating #{self.class}"
  @players.each(&:activate!)
end

#clear_queuevoid

This method returns an undefined value.

Clears the internal queue.


130
131
132
133
# File 'lib/muzak/player/multiplayer.rb', line 130

def clear_queue
  return unless running?
  @players.each(&:clear_queue)
end

#deactivate!void

This method returns an undefined value.

Deactivates each player under the multiplayer.


40
41
42
43
# File 'lib/muzak/player/multiplayer.rb', line 40

def deactivate!
  debug "deactivating #{self.class}"
  @players.each(&:deactivate!)
end

#enqueue_album(album) ⇒ void

Note:

Activates all players if not already activated.

This method returns an undefined value.

Tell all players to add the given album to its queue.

Parameters:

  • album (Album)

    the album to add


94
95
96
97
# File 'lib/muzak/player/multiplayer.rb', line 94

def enqueue_album(album)
  activate! unless running?
  @players.each { |p| p.enqueue_album(album) }
end

#enqueue_playlist(playlist) ⇒ void

Note:

Activates all players if not already activated.

This method returns an undefined value.

Tell all players to add the given playlist to its queue.

Parameters:

  • playlist (Playlist)

    the playlist to add


103
104
105
106
# File 'lib/muzak/player/multiplayer.rb', line 103

def enqueue_playlist(playlist)
  activate! unless running?
  @players.each { |p| p.enqueue_playlist(playlist) }
end

#enqueue_song(song) ⇒ void

Note:

Activates all players if not already activated.

This method returns an undefined value.

Tell all players to add the given song to its queue.

Parameters:

  • song (Song)

    the song to add


85
86
87
88
# File 'lib/muzak/player/multiplayer.rb', line 85

def enqueue_song(song)
  activate! unless running?
  @players.each { |p| p.enqueue_song(song) }
end

#list_queueArray<Song>

Note:

This includes songs already played.

Get the internal queue of the first player.

Returns:

  • (Array<Song>)

    all songs in all players's queue


111
112
113
# File 'lib/muzak/player/multiplayer.rb', line 111

def list_queue
  @players.first.list_queue
end

#next_songvoid

Note:

Does nothing if the current song is the last.

This method returns an undefined value.

Tell all players to play the next song in its queue.


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

def next_song
  @players.each(&:next_song)
end

#now_playingSong?

Get the currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song


137
138
139
140
# File 'lib/muzak/player/multiplayer.rb', line 137

def now_playing
  @players[1].now_playing
  @players.first.now_playing
end

#pausevoid

Note:

Does nothing is playback is already paused.

This method returns an undefined value.

Tell all players to pause playback.


56
57
58
59
# File 'lib/muzak/player/multiplayer.rb', line 56

def pause
  return unless running?
  @players.each(&:pause)
end

#playvoid

Note:

Does nothing is playback is already in progress.

This method returns an undefined value.

Tell all players to begin playback.


48
49
50
51
# File 'lib/muzak/player/multiplayer.rb', line 48

def play
  return unless running?
  @players.each(&:play)
end

#playing?Boolean

Returns Whether or not any of the players are currently playing.

Returns:

  • (Boolean)

    Whether or not any of the players are currently playing.


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

def playing?
  return false unless running?
  @players.any?(&:playing?)
end

#previous_songvoid

Note:

Does nothing if the current song is the first.

This method returns an undefined value.

Tell all players to play the previous song in its queue.


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

def previous_song
  @players.each(&:previous_song)
end

#running?Boolean

Returns whether or not any of the players are currently running.

Returns:

  • (Boolean)

    whether or not any of the players are currently running.


27
28
29
# File 'lib/muzak/player/multiplayer.rb', line 27

def running?
  @players.any?(&:running?)
end

#shuffle_queuevoid

This method returns an undefined value.

Shuffle the internal queue.


117
118
119
120
121
122
123
124
125
126
# File 'lib/muzak/player/multiplayer.rb', line 117

def shuffle_queue
  return unless running?
  # XXX: shuffling is currently done internally within each player,
  # meaning that shuffling within multiplayer would leave each
  # player in an inconsistent queue state.
  # the solution to this is probably to list the queue, shuffle that
  # list, clear the player's queue, and then load the single shuffled
  # list back into each player.
  danger "shuffling doesn't currently make any sense in multiplayer!"
end