Class: MPlayer
- Inherits:
-
Object
- Object
- MPlayer
- Includes:
- ColorDebugMessages, Error
- Defined in:
- lib/easy_mplayer.rb,
lib/easy_mplayer/main.rb,
lib/easy_mplayer/errors.rb,
lib/easy_mplayer/worker.rb,
lib/easy_mplayer/helpers.rb,
lib/easy_mplayer/callback.rb,
lib/easy_mplayer/commands.rb
Defined Under Namespace
Modules: Error Classes: Callback, CallbackList, Command, Worker
Constant Summary collapse
- DEFAULT_OPTS =
all of these can be overridden by passing them to #new
{ :program => '/usr/bin/mplayer', :message_style => :info, :seek_size => 10, :select_wait_time => 1, :thread_safe_callbacks => true }
- DEBUG_MESSAGE_TYPES =
the color_debug_message parameter sets we can switch between, for convenience. (flags for ColorDebugMessages)
{ :quiet => { }, :error_only => { :warn => true }, :info => { :warn => true, :info => true }, :debug => { :warn => true, :info => true, :debug => true, :class_only => false } }
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Class Method Summary collapse
-
.callback(*names, &block) ⇒ Object
register a block with the named callback(s).
-
.class_callbacks ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#callback(*names, &block) ⇒ Object
register a function into each of the named callback chains.
-
#callback!(name, *args) ⇒ Object
call an entire callback chain, passing in a list of args.
-
#create_worker ⇒ Object
:nodoc:.
-
#initialize(new_opts = Hash.new) ⇒ MPlayer
constructor
create a new object.
-
#inspect ⇒ Object
:nodoc:.
-
#pause ⇒ Object
pause playback if we are running.
-
#pause_or_unpause ⇒ Object
use this instead of #pause or #unpause, and the flag will be toggled with each call.
-
#paused? ⇒ Boolean
true if we are running, yet the media has stopped.
-
#play ⇒ Object
spawn the mplayer process, which also starts the media playing.
-
#play_to_end ⇒ Object
a blocking version of #play for trivial uses.
-
#running? ⇒ Boolean
true if the mplayer process is active and running.
-
#seek_by(amount) ⇒ Object
seek by a relative amount, in seconds.
-
#seek_forward(amount = opts[:seek_size]) ⇒ Object
seek forward a given number of seconds, or
opts[:seek_size]
seconds by default. -
#seek_reverse(amount = opts[:seek_size]) ⇒ Object
seek backwards (rewind) by a given number of seconds, or
opts[:seek_size]
seconds by default. -
#seek_start ⇒ Object
reset back to the beginning of the file.
-
#seek_to_percent(percent) ⇒ Object
Seek to an absolute position in a file, by percent of the total size.
-
#seek_to_time(seconds) ⇒ Object
seek to an absolute position in a file, by seconds.
-
#send_command(*args) ⇒ Object
pipe a command to mplayer via slave mode.
-
#set_message_style(type) ⇒ Object
can be any of: :quiet Supperss all output! :error_only Off except for errors :info Also show information messages :debug Heavy debug output (spammy).
-
#stop ⇒ Object
kill off the mplayer process.
-
#unpause ⇒ Object
opposite of #pause.
-
#update_stat(name, newval) ⇒ Object
:nodoc:.
-
#worker ⇒ Object
:nodoc:.
Constructor Details
#initialize(new_opts = Hash.new) ⇒ MPlayer
create a new object. The option hash must have, at a minimum, a :path reference to the file we want to play, or an exception will be raised.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/easy_mplayer/main.rb', line 55 def initialize(new_opts=Hash.new) @opts = DEFAULT_OPTS.merge(new_opts) opts[:message_style] unless File.executable?(@opts[:program]) raise NoPlayerFound.new(@opts[:program]) end unless @opts[:path] and File.readable?(new_opts[:path]) raise NoTargetPath.new(@opts[:path]) end @stats = Hash.new @callbacks = Hash.new @worker = nil self.class.class_callbacks.each do |opts| opts[:scope] = self CallbackList.register opts end end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
31 32 33 |
# File 'lib/easy_mplayer/main.rb', line 31 def callbacks @callbacks end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
31 32 33 |
# File 'lib/easy_mplayer/main.rb', line 31 def opts @opts end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
31 32 33 |
# File 'lib/easy_mplayer/main.rb', line 31 def stats @stats end |
Class Method Details
.callback(*names, &block) ⇒ Object
register a block with the named callback(s). This is the same as the instance-method, generally, but it uses instance_exec to give the block the same scope (the MPlayer instance)
41 42 43 44 45 46 47 48 49 |
# File 'lib/easy_mplayer/main.rb', line 41 def callback(*names, &block) names.each do |name| class_callbacks << { :name => name, :type => :class, :block => block } end end |
.class_callbacks ⇒ Object
:nodoc:
34 35 36 |
# File 'lib/easy_mplayer/main.rb', line 34 def class_callbacks # :nodoc: @@class_callbacks ||= Array.new end |
Instance Method Details
#callback(*names, &block) ⇒ Object
register a function into each of the named callback chains
138 139 140 141 142 |
# File 'lib/easy_mplayer/main.rb', line 138 def callback(*names, &block) names.each do |name| CallbackList.register :name => name, :block => block, :type => :instance end end |
#callback!(name, *args) ⇒ Object
call an entire callback chain, passing in a list of args
132 133 134 135 |
# File 'lib/easy_mplayer/main.rb', line 132 def callback!(name, *args) # :nodoc: #puts "CALLBACK! #{name.inspect} #{args.inspect}" CallbackList.run!(name, args) end |
#create_worker ⇒ Object
:nodoc:
164 165 166 167 168 169 170 |
# File 'lib/easy_mplayer/main.rb', line 164 def create_worker # :nodoc: callback! :creating_worker @worker = Worker.new(self) @stats = Hash.new @paused = false callback! :worker_running end |
#inspect ⇒ Object
:nodoc:
122 123 124 125 126 127 128 129 |
# File 'lib/easy_mplayer/main.rb', line 122 def inspect # :nodoc: vals = [['running', running?], ['paused', paused?]] vals << ['info', stats.inspect] if running? "#<#{self.class} " + vals.map do |x| x.first + '=' + x.last.to_s end.join(' ') + '>' end |
#pause ⇒ Object
pause playback if we are running
26 27 28 29 30 31 32 |
# File 'lib/easy_mplayer/helpers.rb', line 26 def pause return if paused? info "PAUSE!" send_command :pause @paused = true callback! :pause, true end |
#pause_or_unpause ⇒ Object
use this instead of #pause or #unpause, and the flag will be toggled with each call
45 46 47 |
# File 'lib/easy_mplayer/helpers.rb', line 45 def pause_or_unpause paused? ? unpause : pause end |
#paused? ⇒ Boolean
true if we are running, yet the media has stopped
145 146 147 |
# File 'lib/easy_mplayer/main.rb', line 145 def paused? @paused end |
#play ⇒ Object
spawn the mplayer process, which also starts the media playing. It is requires that #opts point to a valid media file
11 12 13 14 15 16 |
# File 'lib/easy_mplayer/helpers.rb', line 11 def play stop if running? info "PLAY: #{opts[:path]}" worker.startup! end |
#play_to_end ⇒ Object
a blocking version of #play for trivial uses. It returns only after the mplayer process finally terminates itself
4 5 6 7 |
# File 'lib/easy_mplayer/helpers.rb', line 4 def play_to_end play sleep 1 while running? end |
#running? ⇒ Boolean
true if the mplayer process is active and running
150 151 152 |
# File 'lib/easy_mplayer/main.rb', line 150 def running? !!@worker and @worker.ok? end |
#seek_by(amount) ⇒ Object
seek by a relative amount, in seconds. requires a float. Negative values rewind to a previous point.
69 70 71 72 |
# File 'lib/easy_mplayer/helpers.rb', line 69 def seek_by(amount) info "SEEK BY: #{amount}" send_command :seek, amount, 0 end |
#seek_forward(amount = opts[:seek_size]) ⇒ Object
seek forward a given number of seconds, or opts[:seek_size]
seconds by default
76 77 78 |
# File 'lib/easy_mplayer/helpers.rb', line 76 def seek_forward(amount = opts[:seek_size]) seek_by(amount) end |
#seek_reverse(amount = opts[:seek_size]) ⇒ Object
seek backwards (rewind) by a given number of seconds, or opts[:seek_size]
seconds by default. Note that a /positive/ value here rewinds!
83 84 85 |
# File 'lib/easy_mplayer/helpers.rb', line 83 def seek_reverse(amount = opts[:seek_size]) seek_by(-amount) end |
#seek_start ⇒ Object
reset back to the beginning of the file
88 89 90 |
# File 'lib/easy_mplayer/helpers.rb', line 88 def seek_start seek_to_percent(0.0) end |
#seek_to_percent(percent) ⇒ Object
Seek to an absolute position in a file, by percent of the total size. requires a float argument, that is (0.0 <= percent <= 100.0)
51 52 53 54 55 56 57 58 |
# File 'lib/easy_mplayer/helpers.rb', line 51 def seek_to_percent(percent) return if percent.to_i == @stats[:position] percent = percent.to_f percent = 0.0 if percent < 0 percent = 100.0 if percent > 100 info "SEEK TO: #{percent}%" send_command :seek, percent, 1 end |
#seek_to_time(seconds) ⇒ Object
seek to an absolute position in a file, by seconds. requires a float between 0.0 and the length (in seconds) of the file being played.
62 63 64 65 |
# File 'lib/easy_mplayer/helpers.rb', line 62 def seek_to_time(seconds) info "SEEK TO: #{seconds} seconds" send_command :seek, seconds, 1 end |
#send_command(*args) ⇒ Object
pipe a command to mplayer via slave mode
155 156 157 |
# File 'lib/easy_mplayer/main.rb', line 155 def send_command(*args) worker.send_command(*args) end |
#set_message_style(type) ⇒ Object
can be any of:
:quiet Supperss all output!
:error_only Off except for errors
:info Also show information messages
:debug Heavy debug output (spammy)
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/easy_mplayer/main.rb', line 109 def (type) hsh = DEBUG_MESSAGE_TYPES[type.to_sym] or raise BadMsgType.new(type.inspect) hsh = hsh.dup hsh[:debug] ||= false hsh[:info] ||= false hsh[:warn] ||= false hsh[:class_only] ||= true hsh[:prefix_only] ||= false ColorDebugMessages.global_debug_flags(hsh) opts[:message_style] = type end |
#stop ⇒ Object
kill off the mplayer process. This invalidates any running media, though it can be restarted again with another call to #play
20 21 22 23 |
# File 'lib/easy_mplayer/helpers.rb', line 20 def stop info "STOP!" @worker.shutdown! if @worker end |
#unpause ⇒ Object
opposite of #pause
35 36 37 38 39 40 41 |
# File 'lib/easy_mplayer/helpers.rb', line 35 def unpause return unless paused? info "UNPAUSE!" send_command :pause @paused = false callback! :unpause, false end |
#update_stat(name, newval) ⇒ Object
:nodoc:
172 173 174 175 176 177 178 179 |
# File 'lib/easy_mplayer/main.rb', line 172 def update_stat(name, newval) # :nodoc: name = name.to_sym if @stats[name] != newval debug "STATS[:#{name}] -> #{newval.inspect}" @stats[name] = newval callback! name, newval end end |
#worker ⇒ Object
:nodoc:
159 160 161 162 |
# File 'lib/easy_mplayer/main.rb', line 159 def worker # :nodoc: create_worker if @worker.nil? @worker end |