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



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

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



8
9
10
# File 'lib/muzak/player/multiplayer.rb', line 8

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



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

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.



31
32
33
34
# File 'lib/muzak/player/multiplayer.rb', line 31

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

#clear_queuevoid

This method returns an undefined value.

Clears the internal queue.



128
129
130
131
# File 'lib/muzak/player/multiplayer.rb', line 128

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.



38
39
40
41
# File 'lib/muzak/player/multiplayer.rb', line 38

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



92
93
94
95
# File 'lib/muzak/player/multiplayer.rb', line 92

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



101
102
103
104
# File 'lib/muzak/player/multiplayer.rb', line 101

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



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

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



109
110
111
# File 'lib/muzak/player/multiplayer.rb', line 109

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.



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

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

#now_playingSong?

Get the currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song



135
136
137
138
# File 'lib/muzak/player/multiplayer.rb', line 135

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.



54
55
56
57
# File 'lib/muzak/player/multiplayer.rb', line 54

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.



46
47
48
49
# File 'lib/muzak/player/multiplayer.rb', line 46

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.



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

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.



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

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.



25
26
27
# File 'lib/muzak/player/multiplayer.rb', line 25

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

#shuffle_queuevoid

This method returns an undefined value.

Shuffle the internal queue.



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

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