class Muzak::Player::MultiPlayer

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.

Attributes

players[R]

@return [Array<StubPlayer>] the players associated with this multiplayer

Public Class Methods

available?() click to toggle source

@return [Boolean] whether or not all of the players are available

# 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
new(instance) click to toggle source

@param instance [Instance] the instance associated with the player

Calls superclass method
# 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

Public Instance Methods

activate!() click to toggle source

Activates each player under the multiplayer. @return [void]

# File lib/muzak/player/multiplayer.rb, line 33
def activate!
  debug "activating #{self.class}"
  @players.each(&:activate!)
end
clear_queue() click to toggle source

Clears the internal queue. @return [void]

# File lib/muzak/player/multiplayer.rb, line 130
def clear_queue
  return unless running?
  @players.each(&:clear_queue)
end
deactivate!() click to toggle source

Deactivates each player under the multiplayer. @return [void]

# File lib/muzak/player/multiplayer.rb, line 40
def deactivate!
  debug "deactivating #{self.class}"
  @players.each(&:deactivate!)
end
enqueue_album(album) click to toggle source

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

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

Tell all players to add the given playlist to its queue. @param playlist [Playlist] the playlist to add @return [void] @note Activates all players if not already activated.

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

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

# File lib/muzak/player/multiplayer.rb, line 85
def enqueue_song(song)
  activate! unless running?
  @players.each { |p| p.enqueue_song(song) }
end
list_queue() click to toggle source

Get the internal queue of the first player. @return [Array<Song>] all songs in all players's queue @note This includes songs already played.

# File lib/muzak/player/multiplayer.rb, line 111
def list_queue
  @players.first.list_queue
end
next_song() click to toggle source

Tell all players to play the next song in its queue. @return [void] @note Does nothing if the current song is the last.

# File lib/muzak/player/multiplayer.rb, line 70
def next_song
  @players.each(&:next_song)
end
now_playing() click to toggle source

Get the currently loaded song. @return [Song, nil] the currently loaded song

# File lib/muzak/player/multiplayer.rb, line 137
def now_playing
  @players[1].now_playing
  @players.first.now_playing
end
pause() click to toggle source

Tell all players to pause playback. @return [void] @note Does nothing is playback is already paused.

# File lib/muzak/player/multiplayer.rb, line 56
def pause
  return unless running?
  @players.each(&:pause)
end
play() click to toggle source

Tell all players to begin playback. @return [void] @note Does nothing is playback is already in progress.

# File lib/muzak/player/multiplayer.rb, line 48
def play
  return unless running?
  @players.each(&:play)
end
playing?() click to toggle source

@return [Boolean] Whether or not any of the players are currently playing.

# File lib/muzak/player/multiplayer.rb, line 62
def playing?
  return false unless running?
  @players.any?(&:playing?)
end
previous_song() click to toggle source

Tell all players to play the previous song in its queue. @return [void] @note Does nothing if the current song is the first.

# File lib/muzak/player/multiplayer.rb, line 77
def previous_song
  @players.each(&:previous_song)
end
running?() click to toggle source

@return [Boolean] whether or not any of the players are currently running.

# File lib/muzak/player/multiplayer.rb, line 27
def running?
  @players.any?(&:running?)
end
shuffle_queue() click to toggle source

Shuffle the internal queue. @return [void]

# 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