Class: Airplay::Player
- Inherits:
-
Object
- Object
- Airplay::Player
- Extended by:
- Forwardable
- Includes:
- Celluloid
- Defined in:
- lib/airplay/player.rb,
lib/airplay/player/media.rb,
lib/airplay/player/timers.rb,
lib/airplay/player/playlist.rb,
lib/airplay/player/playback_info.rb
Overview
Public: The class that handles all the video playback
Defined Under Namespace
Classes: Media, PlaybackInfo, Playlist, Timers
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Public: Cleans up the player.
-
#info ⇒ Object
Public: checks current playback information.
-
#initialize(device) ⇒ Player
constructor
A new instance of Player.
- #loading? ⇒ Boolean
-
#next ⇒ Object
Public: Plays the next video in the playlist.
-
#pause ⇒ Object
Public: Pauses a playing video.
- #paused? ⇒ Boolean
-
#play(media_to_play = "playlist", options = {}) ⇒ Object
Public: Plays a given url or file.
- #played? ⇒ Boolean
- #playing? ⇒ Boolean
-
#playlist ⇒ Object
Public: Gets the current playlist.
-
#playlists ⇒ Object
Public: Gets all the playlists.
-
#previous ⇒ Object
Public: Plays the previous video in the playlist.
-
#progress(callback) ⇒ Object
Public: Handles the progress of the playback, the given &block get’s executed every second while the video is played.
-
#resume ⇒ Object
Public: Resumes a paused video.
-
#scrub ⇒ Object
Public: Shows the current playback time if a video is being played.
-
#seek(position) ⇒ Object
Public: Seeks to the specified position (seconds) in the video.
-
#stop ⇒ Object
Public: Stops the video.
- #stopped? ⇒ Boolean
-
#use(name) ⇒ Object
Public: Sets a given playlist.
-
#wait ⇒ Object
Public: Locks the execution until the video gets fully played.
Constructor Details
#initialize(device) ⇒ Player
Returns a new instance of Player.
25 26 27 |
# File 'lib/airplay/player.rb', line 25 def initialize(device) @device = device end |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
23 24 25 |
# File 'lib/airplay/player.rb', line 23 def device @device end |
Instance Method Details
#cleanup ⇒ Object
Public: Cleans up the player
Returns nothing
200 201 202 203 |
# File 'lib/airplay/player.rb', line 200 def cleanup timers.cancel persistent.close end |
#info ⇒ Object
Public: checks current playback information
Returns a PlaybackInfo object with the playback information
142 143 144 145 146 147 |
# File 'lib/airplay/player.rb', line 142 def info response = connection.get("/playback-info").response plist = CFPropertyList::List.new(data: response.body) hash = CFPropertyList.native_types(plist.value) PlaybackInfo.new(hash) end |
#loading? ⇒ Boolean
181 |
# File 'lib/airplay/player.rb', line 181 def loading?; state == :loading end |
#next ⇒ Object
Public: Plays the next video in the playlist
Returns the video that was selected or nil if none
111 112 113 114 115 |
# File 'lib/airplay/player.rb', line 111 def next video = playlist.next play(video) if video video end |
#pause ⇒ Object
Public: Pauses a playing video
Returns nothing
161 162 163 |
# File 'lib/airplay/player.rb', line 161 def pause connection.async.post("/rate?value=0") end |
#paused? ⇒ Boolean
183 |
# File 'lib/airplay/player.rb', line 183 def paused?; state == :paused end |
#play(media_to_play = "playlist", options = {}) ⇒ Object
Public: Plays a given url or file.
Creates a new persistent connection to ensure that
the socket will be kept alive
file_or_url - The url or file to be reproduced options - Optional starting time
Returns nothing
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/airplay/player.rb', line 69 def play(media_to_play = "playlist", = {}) start_the_machine check_for_playback_status media = case true when media_to_play.is_a?(Media) then media_to_play when media_to_play == "playlist" && playlist.any? playlist.next else Media.new(media_to_play) end content = { "Content-Location" => media, "Start-Position" => .fetch(:time, 0.0) } data = content.map { |k, v| "#{k}: #{v}" }.join("\r\n") response = persistent.async.post("/play", data + "\r\n", { "Content-Type" => "text/parameters" }) timers.reset end |
#played? ⇒ Boolean
184 |
# File 'lib/airplay/player.rb', line 184 def played?; state == :played end |
#playing? ⇒ Boolean
182 |
# File 'lib/airplay/player.rb', line 182 def ; state == :playing end |
#playlist ⇒ Object
Public: Gets the current playlist
Returns the first Playlist if none defined or creates a new one
41 42 43 44 45 46 47 48 |
# File 'lib/airplay/player.rb', line 41 def playlist @_playlist ||= if playlists.any? key, value = playlists.first value else Playlist.new("Default") end end |
#playlists ⇒ Object
Public: Gets all the playlists
Returns the Playlists
33 34 35 |
# File 'lib/airplay/player.rb', line 33 def playlists @_playlists ||= Hash.new { |h,k| h[k] = Playlist.new(k) } end |
#previous ⇒ Object
Public: Plays the previous video in the playlist
Returns the video that was selected or nil if none
121 122 123 124 125 |
# File 'lib/airplay/player.rb', line 121 def previous video = playlist.previous play(video) if video video end |
#progress(callback) ⇒ Object
Public: Handles the progress of the playback, the given &block get’s
executed every second while the video is played.
&block - Block to be executed in every playable second.
Returns nothing
101 102 103 104 105 |
# File 'lib/airplay/player.rb', line 101 def progress(callback) timers << every(1) do callback.call(info) if end end |
#resume ⇒ Object
Public: Resumes a paused video
Returns nothing
153 154 155 |
# File 'lib/airplay/player.rb', line 153 def resume connection.async.post("/rate?value=1") end |
#scrub ⇒ Object
Public: Shows the current playback time if a video is being played.
Returns a hash with the :duration and current :position
131 132 133 134 135 136 |
# File 'lib/airplay/player.rb', line 131 def scrub return unless response = connection.get("/scrub").response parts = response.body.split("\n") Hash[parts.collect { |v| v.split(": ") }] end |
#seek(position) ⇒ Object
Public: Seeks to the specified position (seconds) in the video
Returns nothing
177 178 179 |
# File 'lib/airplay/player.rb', line 177 def seek(position) connection.async.post("/scrub?position=#{position}") end |
#stop ⇒ Object
Public: Stops the video
Returns nothing
169 170 171 |
# File 'lib/airplay/player.rb', line 169 def stop connection.post("/stop") end |
#stopped? ⇒ Boolean
185 |
# File 'lib/airplay/player.rb', line 185 def stopped?; state == :stopped end |
#use(name) ⇒ Object
Public: Sets a given playlist
name - The name of the playlist to be used
Returns nothing
56 57 58 |
# File 'lib/airplay/player.rb', line 56 def use(name) @_playlist = playlists[name] end |
#wait ⇒ Object
Public: Locks the execution until the video gets fully played
Returns nothing
191 192 193 194 |
# File 'lib/airplay/player.rb', line 191 def wait sleep 1 while wait_for_playback? cleanup end |