Class: Mumbletune::HallonPlayer
- Inherits:
-
Object
- Object
- Mumbletune::HallonPlayer
- Defined in:
- lib/mumbletune/hallon_player.rb
Instance Attribute Summary collapse
-
#add_history ⇒ Object
Returns the value of attribute add_history.
-
#audio_queue ⇒ Object
Returns the value of attribute audio_queue.
-
#current_track ⇒ Object
Returns the value of attribute current_track.
-
#play_history ⇒ Object
Returns the value of attribute play_history.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#ready ⇒ Object
Returns the value of attribute ready.
Instance Method Summary collapse
-
#add_collection(col, now = false) ⇒ Object
Queue Control.
- #any? ⇒ Boolean
- #clear_queue ⇒ Object
- #connect ⇒ Object
- #disconnect ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ HallonPlayer
constructor
A new instance of HallonPlayer.
- #next ⇒ Object
- #pause ⇒ Object
- #paused? ⇒ Boolean
-
#play ⇒ Object
Playback Commands.
-
#play_token_lost ⇒ Object
Callback Handling.
-
#playing? ⇒ Boolean
Status methods.
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #undo ⇒ Object
Constructor Details
#initialize ⇒ HallonPlayer
Returns a new instance of HallonPlayer.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mumbletune/hallon_player.rb', line 11 def initialize conf = Mumbletune.config @play_history = Array.new @add_history = Array.new @queue = Array.new @prev_id = 0 @ready = false @audio_queue = Queue.new @ready = true end |
Instance Attribute Details
#add_history ⇒ Object
Returns the value of attribute add_history.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def add_history @add_history end |
#audio_queue ⇒ Object
Returns the value of attribute audio_queue.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def audio_queue @audio_queue end |
#current_track ⇒ Object
Returns the value of attribute current_track.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def current_track @current_track end |
#play_history ⇒ Object
Returns the value of attribute play_history.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def play_history @play_history end |
#queue ⇒ Object
Returns the value of attribute queue.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def queue @queue end |
#ready ⇒ Object
Returns the value of attribute ready.
9 10 11 |
# File 'lib/mumbletune/hallon_player.rb', line 9 def ready @ready end |
Instance Method Details
#add_collection(col, now = false) ⇒ Object
Queue Control
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mumbletune/hallon_player.rb', line 76 def add_collection(col, now=false) only_track = empty? # add to the queue if now @queue.unshift col else @queue.push col end # record in additions history @add_history.push col # play it, if this is the first track or if the user specified `now` self.next if now || only_track end |
#any? ⇒ Boolean
65 66 67 68 69 |
# File 'lib/mumbletune/hallon_player.rb', line 65 def any? cols_with_more = @queue.map { |col| col.any? } cols_with_more.delete_if { |m| m == false } cols_with_more.any? end |
#clear_queue ⇒ Object
100 101 102 103 |
# File 'lib/mumbletune/hallon_player.rb', line 100 def clear_queue @queue.clear self.stop end |
#connect ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mumbletune/hallon_player.rb', line 24 def connect conf = Mumbletune.config @session = Hallon::Session.initialize(IO.read(conf["spotify"]["appkey"])) @session.login!(conf["spotify"]["username"], conf["spotify"]["password"]) @player = Hallon::Player.new(Hallon::QueueOutput) do # Set driver options @driver.queue = Mumbletune.player.audio_queue @driver.verbose = true if Mumbletune.verbose # Define callbacks on(:end_of_track) { Mumbletune.player.next } on(:streaming_error) { |s, e| raise "Spotify connection error: #{e}" } on(:play_token_lost) { Mumbletune.player.play_token_lost } end @ready = true start_event_loop end |
#disconnect ⇒ Object
46 47 48 49 50 |
# File 'lib/mumbletune/hallon_player.rb', line 46 def disconnect @logging_out = true @event_loop_thread.kill @session.logout! end |
#empty? ⇒ Boolean
71 72 73 |
# File 'lib/mumbletune/hallon_player.rb', line 71 def empty? !any? end |
#next ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mumbletune/hallon_player.rb', line 122 def next # move the collection to play_history if it has played all its tracks @play_history << @queue.shift if @queue.first && @queue.first.empty? return stop unless any? track = @queue.first.next return stop unless track # play that shit! @current_track = track @player.play track puts "\u266B #{track.name} - #{track.artist.name}" unless Mumbletune.verbose end |
#pause ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/mumbletune/hallon_player.rb', line 114 def pause if @player.pause elsif paused? @player.play end end |
#paused? ⇒ Boolean
57 58 59 |
# File 'lib/mumbletune/hallon_player.rb', line 57 def paused? @player.status == :paused end |
#play ⇒ Object
Playback Commands
106 107 108 109 110 111 112 |
# File 'lib/mumbletune/hallon_player.rb', line 106 def play if stopped? self.next elsif paused? @player.play end end |
#play_token_lost ⇒ Object
Callback Handling
144 145 146 147 148 |
# File 'lib/mumbletune/hallon_player.rb', line 144 def play_token_lost Mumbletune.mumble.broadcast %w{Mumbletune was just paused because this Spotify account is being used elsewhere. Type <code>unpause </code> to regain control and keep playing.} * " " end |
#playing? ⇒ Boolean
Status methods
53 54 55 |
# File 'lib/mumbletune/hallon_player.rb', line 53 def @player.status == :playing end |
#stop ⇒ Object
139 140 141 |
# File 'lib/mumbletune/hallon_player.rb', line 139 def stop @player.stop end |
#stopped? ⇒ Boolean
61 62 63 |
# File 'lib/mumbletune/hallon_player.rb', line 61 def stopped? @player.status == :stopped end |
#undo ⇒ Object
93 94 95 96 97 98 |
# File 'lib/mumbletune/hallon_player.rb', line 93 def undo removed = @add_history.pop @queue.delete_if { |col| col == removed } self.next if removed.current_track removed end |