Created
July 30, 2012 16:08
-
-
Save b1nary/3208065 to your computer and use it in GitHub Desktop.
Control a mplayer slave within ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Mplayer controlling class | |
# | |
# Initialize with music file | |
# Good Slave reference: | |
# https://github.com/CodeMonkeySteve/ruby-mplayer/blob/master/docs/mplayer-slave.txt | |
# | |
# License: WTFPL | |
# | |
class MPlayer | |
# Helper function | |
def is_numeric?(s) | |
!!Float(s) rescue false | |
end | |
def initialize file | |
@player = IO.popen("mplayer -quiet -noconsolecontrols -nolirc -idle -slave #{file}", "w+") | |
# Forward intro text | |
get = @player.gets | |
while !get.include? 'Starting playback...' | |
get = @player.gets | |
end | |
@paused = false | |
@muted = false | |
end | |
def gets | |
out = @player.gets | |
while out.chomp == "" or out.include? "\r\e" | |
out = @player.gets | |
end | |
out | |
end | |
# pause | |
# none|true|false (none toggles) | |
def pause val = nil | |
if val == true | |
(@player.puts "pause"; @paused = true ) if @paused == false | |
elsif val == false | |
(@player.puts "pause"; @paused = false) if @paused | |
else | |
if @paused == true; @paused = false; else; @paused = true; end | |
@player.puts "pause" | |
end | |
end | |
# mute true|false | |
def mute val = true | |
@muted = val | |
val = 1 if val == true | |
val = 0 if val == false | |
@player.puts "mute #{val}" | |
end | |
# seek | |
def seek val | |
if self.is_numeric? val | |
@player.puts "seek #{val}" | |
end | |
end | |
# speed | |
def speed val | |
if self.is_numeric? val | |
@player.puts "speed_set #{val}" | |
end | |
end | |
# volume | |
# NUM|:up|:down | |
def volume val | |
if self.is_numeric? val | |
@player.puts "volume #{val} 1" | |
else | |
@player.puts "volume -1" if val == :down | |
@player.puts "volume 1" if val == :up | |
end | |
end | |
# quit player | |
def quit | |
@player.puts "quit" | |
end | |
# load file (quits current) | |
def load file, append = false | |
if append | |
@player.puts "loadfile #{file} 1" if File.exists? file | |
else | |
@player.puts "loadfile #{file}" if File.exists? file | |
end | |
end | |
# current playing filename | |
def filename | |
@player.puts "get_file_name" | |
self.gets.chomp | |
end | |
# current playing title | |
def title | |
@player.puts "get_meta_title" | |
self.gets.chomp | |
end | |
# get length | |
def length | |
@player.puts "get_time_length" | |
self.gets.chomp().split("=")[1].to_f | |
end | |
# get current position | |
def position | |
@player.puts "get_time_pos" | |
self.gets.chomp().split("=")[1].to_f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment