Class: Airplay::Player

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#deviceObject (readonly)

Returns the value of attribute device.



23
24
25
# File 'lib/airplay/player.rb', line 23

def device
  @device
end

Instance Method Details

#cleanupObject

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

#infoObject

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

Returns:

  • (Boolean)


181
# File 'lib/airplay/player.rb', line 181

def loading?; state == :loading end

#nextObject

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

#pauseObject

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

Returns:

  • (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", options = {})
  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" => options.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

Returns:

  • (Boolean)


184
# File 'lib/airplay/player.rb', line 184

def played?;  state == :played  end

#playing?Boolean

Returns:

  • (Boolean)


182
# File 'lib/airplay/player.rb', line 182

def playing?; state == :playing end

#playlistObject

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

#playlistsObject

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

#previousObject

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 playing?
  end
end

#resumeObject

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

#scrubObject

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 playing?
  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

#stopObject

Public: Stops the video

Returns nothing



169
170
171
# File 'lib/airplay/player.rb', line 169

def stop
  connection.post("/stop")
end

#stopped?Boolean

Returns:

  • (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

#waitObject

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