lib/radiodan/player.rb in radiodan-0.0.4 vs lib/radiodan/player.rb in radiodan-1.0.0
- old
+ new
@@ -8,12 +8,11 @@
extend Forwardable
include Logging
include EventBinding
attr_reader :adapter, :playlist
- def_delegators :adapter, :stop
-
+
def adapter=(adapter)
@adapter = adapter
@adapter.player = self
end
@@ -21,50 +20,78 @@
!adapter.nil?
end
def playlist=(new_playlist)
@playlist = new_playlist
- trigger_event(:playlist, @playlist)
- # run sync to explicitly conform to new playlist?
+ trigger_event(:playlist, @playlist)
+ trigger_event(:player_state, @adapter.playlist) if @adapter
+
@playlist
end
+ def state
+ adapter.playlist
+ end
+
=begin
Sync checks the current status of the player.
Is it paused? Playing? What is it playing?
It compares the expected to actual statuses and
- makes changes required to keep them the same.
+ triggers events if there is a difference.
=end
def sync
return false unless adapter?
current = adapter.playlist
expected = playlist
- state = Radiodan::PlaylistSync.new expected, current
+ sync = Radiodan::PlaylistSync.new expected, current
+ synced = sync.sync?
- if state.sync?
- true
- else
+ unless synced
# playback state
- if state.errors.include? :state
- logger.debug "Expected: #{expected.state} Got: #{current.state}"
- trigger_event :play_state, expected.state
+ sync.errors.each do |e|
+ case e
+ when :state
+ logger.debug "Expected State: #{expected.state} Got: #{current.state}"
+ trigger_event :play_state, current.state
+ when :mode
+ logger.debug "Expected Mode: #{expected.mode} Got: #{current.mode}"
+ trigger_event :play_mode, current.mode
+ when :new_tracks
+ logger.debug "Expected: #{expected.current.inspect} Got: #{current.current.inspect}"
+ trigger_event :playlist, expected
+ when :add_tracks
+ logger.debug "Found additional tracks to enqueue"
+ trigger_event :enqueue, expected.tracks[current.tracks.size..-1]
+ trigger_event :play_pending if sync.errors.include?(:state) && current.state == :stop
+ when :volume
+ logger.debug "Expected Volume: #{expected.volume} Got: #{current.volume}"
+ trigger_event :volume, expected.volume
+ end
end
-
- if state.errors.include? :mode
- logger.debug "Expected: #{expected.mode} Got: #{current.mode}"
- trigger_event :play_mode, expected.mode
- end
-
- # playlist
- if state.errors.include? :playlist
- logger.debug "Expected: #{expected.current} Got: #{current.current}"
- trigger_event :playlist, expected
- end
-
- false
+ end
+
+ trigger_event :sync, current
+ synced
+ end
+
+ def respond_to?(method)
+ if adapter.respond_to? method
+ true
+ else
+ super
+ end
+ end
+
+ private
+
+ def method_missing(method, *args, &block)
+ if adapter.respond_to? method
+ adapter.send method, *args, &block
+ else
+ super
end
end
end
end