class MVLC::Player::Wrapper
Wrapper
for video player functionality
Constants
- VOLUME_FACTOR
Attributes
Public Class Methods
@param [Hash] options
# File lib/mvlc/player/wrapper.rb, line 15 def initialize(options = {}) @callback = {} @state = State.new @player = initialize_player @pid = player_pid end
Public Instance Methods
Is the video player active? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 61 def active? !@player.nil? end
Add a callback to be called at the end of playback of a media file @param [Proc] block @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 94 def add_end_of_file_callback(&block) @callback[:end_of_file] = block true end
Add a callback to be called when progress is updated during playback @param [Proc] block @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 86 def add_progress_callback(&block) @callback[:progress] = block true end
Add all of the video player methods to the wrapper as instructions
# File lib/mvlc/player/wrapper.rb, line 107 def method_missing(method, *args, &block) if @player.respond_to?(method) @player.send(method, *args, &block) else super end end
Toggles pause @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 67 def pause @state.toggle_pause @player.pause @state.pause? end
Play a media file @param [String] file @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 49 def play(file) if @player.nil? false else @player.play(file) handle_start true end end
Handle events while the player is running @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 75 def playback_loop loop do handle_playback_events sleep(0.05) end true end
Exit the video player @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 101 def quit kill_player true end
Add all of the video player methods to the wrapper as instructions
# File lib/mvlc/player/wrapper.rb, line 116 def respond_to_missing?(method, include_private = false) super || @player.respond_to?(method) end
Seek to the given percent in the currently playing file Value is expected to be 0..100 @param [Integer] value @return [Integer]
# File lib/mvlc/player/wrapper.rb, line 37 def seek_percent(percent) begin length_in_seconds = @player.length seconds = length_in_seconds * (percent / 100.0) @player.seek(seconds) rescue VLC::ReadTimeoutError, VLC::BrokenConnectionError end end
Set the volume level to the given value. Value is expected to be 0..100 but this is not strictly enforced @param [Integer] value @return [Integer]
# File lib/mvlc/player/wrapper.rb, line 26 def volume=(value) @state.volume = value * VOLUME_FACTOR @player.volume(@state.volume) @state.volume end
Private Instance Methods
Has the end of a media file been reached? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 218 def eof? @state.eof_possible? && !playing? end
Has an EOF callback been specified? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 170 def eof_callback? !@callback[:end_of_file].nil? end
Handle the end of playback for a single media file @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 193 def handle_eof @callback[:end_of_file].call @state.handle_eof true end
Should an EOF callback be fired? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 176 def handle_eof? eof? && eof_callback? end
Fire playback event callbacks when appropriate during the playback loop @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 125 def handle_playback_events handle_eof if handle_eof? handle_progress if handle_progress? true end
Fire the progress callback. Used during the playback loop @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 182 def handle_progress begin @callback[:progress].call(@player.progress) true rescue VLC::ReadTimeoutError false end end
Should a progress callback be fired? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 158 def handle_progress? @state.progressing? && progress_callback? end
Handle the beginning of playback for a single media file @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 201 def handle_start @state.handle_start true end
Instantiate the VLC player instance @return [VLC::System]
# File lib/mvlc/player/wrapper.rb, line 133 def initialize_player VLC::System.new(headless: true) end
Kill the VLC player instance @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 145 def kill_player begin @player.connection.write("quit") rescue Errno::EBADF end # TODO: Process.kill not working here `kill -9 #{@pid}` unless @pid.nil? @player.server.stop true end
The OS PID of the player @return [Integer]
# File lib/mvlc/player/wrapper.rb, line 139 def player_pid @player.server.instance_variable_get("@pid") end
Is media playing? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 208 def playing? begin @player.playing? rescue VLC::ReadTimeoutError, VLC::BrokenConnectionError false end end
Has a progress callback been specified? @return [Boolean]
# File lib/mvlc/player/wrapper.rb, line 164 def progress_callback? !@callback[:progress].nil? end