class MMPlayer::Player::Wrapper
Wrapper
for MPlayer functionality
Attributes
Public Class Methods
@param [Hash] options @option options [String] :flags MPlayer command-line flags to use on startup
# File lib/mmplayer/player/wrapper.rb, line 12 def initialize(options = {}) @invoker = Invoker.new(options) @messenger = Messenger.new @callback = {} @state = State.new @threads = [] end
Public Instance Methods
Is MPlayer active? @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 38 def active? !(@player ||= @invoker.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/mmplayer/player/wrapper.rb, line 74 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/mmplayer/player/wrapper.rb, line 66 def add_progress_callback(&block) @callback[:progress] = block true end
Does the MPlayer respond to the given message? @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 93 def mplayer_respond_to?(method, include_private = false) (@player.nil? && MPlayer::Slave.method_defined?(method)) || @player.respond_to?(method) end
Shortcut to send a message to the MPlayer @return [Object]
# File lib/mmplayer/player/wrapper.rb, line 81 def mplayer_send(method, *args, &block) if @player.nil? && MPlayer::Slave.method_defined?(method) # warn else @messenger.send_message do @player.send(method, *args, &block) end end end
Toggles pause @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 44 def pause @state.toggle_pause @player.pause @state.pause? end
Play a media file @param [String] file @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 23 def play(file) @player ||= @invoker.ensure_invoked(file, @state) if @player.nil? false else @threads << ::MMPlayer::Thread.new(:timeout => 2) do @player.load_file(file) handle_start end true end end
Handle events while the player is running @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 52 def playback_loop loop do if handle_progress? @threads << ::MMPlayer::Thread.new { handle_progress } end handle_eof if handle_eof? sleep(0.05) end true end
Cause MPlayer to exit @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 100 def quit @player.quit @threads.each(&:kill) @invoker.destroy true end
Private Instance Methods
Has the end of a media file been reached? @return [Boolean]
# File lib/mmplayer/player/wrapper.rb, line 127 def eof? @state.eof_reached? && get_player_output.size < 1 end
# File lib/mmplayer/player/wrapper.rb, line 117 def eof_callback? !@callback[:end_of_file].nil? end
Poll a single MPlayer value for the given key
# File lib/mmplayer/player/wrapper.rb, line 188 def get_mplayer_float(key) result = @player.get(key) result.strip.to_f end
Get progress percentage from the MPlayer report
# File lib/mmplayer/player/wrapper.rb, line 162 def get_percentage(report) percent = (report[:position] / report[:length]) * 100 percent.round end
Get player output from stdout
# File lib/mmplayer/player/wrapper.rb, line 132 def get_player_output @player.stdout.gets.inspect.strip.gsub(/(\\n|[\\"])/, '').strip end
Handle the end of playback for a single media file
# File lib/mmplayer/player/wrapper.rb, line 145 def handle_eof # do this check again for thread safety if @state.eof_reached? STDOUT.flush @callback[:end_of_file].call @state.handle_eof end true end
# File lib/mmplayer/player/wrapper.rb, line 121 def handle_eof? eof? && eof_callback? end
# File lib/mmplayer/player/wrapper.rb, line 136 def handle_progress poll_mplayer_progress do |time| time[:percent] = get_percentage(time) # do the check again for thread safety @callback[:progress].call(time) if handle_progress? end end
# File lib/mmplayer/player/wrapper.rb, line 109 def handle_progress? @state.progressing? && progress_callback? end
Handle the beginning of playback for a single media file
# File lib/mmplayer/player/wrapper.rb, line 156 def handle_start loop until get_player_output.size > 1 @state.handle_start end
Poll MPlayer for progress information Media progress information eg {
:length => 90.3, :percent => 44, :position => 40.1
} Length and position are in seconds
# File lib/mmplayer/player/wrapper.rb, line 175 def poll_mplayer_progress(&block) time = nil @messenger.send_message do time = { :length => get_mplayer_float("time_length"), :position => get_mplayer_float("time_pos") } yield(time) end time end
# File lib/mmplayer/player/wrapper.rb, line 113 def progress_callback? !@callback[:progress].nil? end