class MPlayer
Attributes
Public Class Methods
returns an instance of MPlayer
. The fifo-file to control the related mplayer-process is created. Also an mplayer-instance gests startet.
Some Options can be used to describe different asspects of the object. They can be set in the options-hash. Following options are avaiable:
-
fifo: path to the fifo-file which will be created
@param options [Hash] Specifies some options for the mplayer-instance. @return [MPlayer] New MPlayer-object
# File lib/mplayer.rb, line 17 def initialize options={fifo: "/tmp/mplayer_rb_#{$$}_fifo"} @options = options @command = "mplayer -slave -idle -quiet -input file=#{options[:fifo]}" @log = "" @lock = Mutex.new create_fifo start_mplayer end
Public Instance Methods
dynamically creates and calls methods when the missing method is a mplayer-command (see COMMANDS)
# File lib/mplayer.rb, line 65 def method_missing name,*args if COMMANDS.include? name with_args = Proc.new do |params| if params.is_a? Array params = params.map do |param| if param.start_with? "/" param = "'" + param + "'" end end params = params.join " " end run "#{name.to_s} #{params}" end argless = Proc.new do run "#{name.to_s}" end if args.empty? self.class.send :define_method, name, &argless self.send name else self.class.send :define_method, name, &with_args self.send name, args end else super name,args end end
override respond_to_missing?, so when a method is called, whichs name is a mplayer-command, the object should respond to it. When not, the standard-behaviour is executed. {Object#respond_to?}
# File lib/mplayer.rb, line 56 def respond_to_missing? method,priv if COMMANDS.include? method return true else super method,priv end end
sends a command to the FIFO-file, which controlls the related mplayer-instance. Example: mp = MPlayer.new
mp.run(“pause) <–send pause-command to mplayer
@params cmd [String] The command which should be sent to mplayer @return nil at the moment @TODO:it should return the output of mplayer for the sent command
# File lib/mplayer.rb, line 36 def run(cmd) begin @stdout.read_nonblock 1500 rescue end File.open(@options[:fifo],"w+") do |f| f.puts cmd end begin sleep 0.1 response = @stdout.read_nonblock 1500 rescue response = "" end response end
Private Instance Methods
creates the fifo-file which will be used to controll the mplayer-instance. It used the path which was specified in the options in new
@return [Fixnum] The process-id of the mkfifo-command
# File lib/mplayer.rb, line 99 def create_fifo system "mkfifo #{@options[:fifo]}" end
kill_mplayer
kills the related mplayer-instance and deletes all created files.
@return [Boolean] True if the mplayer-instance-kill-command succeeded
# File lib/mplayer.rb, line 106 def kill_mplayer File.delete(@options[:fifo]) if File.exists? @options[:fifo] `kill -9 #{@player_pid}` thread.join end
starts the related mplayer-instance and stores the pid of it in the player_pid-attribute
@return [Fixnum] The pid of the related mplayer-instance
# File lib/mplayer.rb, line 115 def start_mplayer at_exit do kill_mplayer end @stdin,@stdout,@thread = Open3.popen2e(@command) @player_pid = @thread.pid end