Class: VideoPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/video_player.rb

Constant Summary collapse

MP_ARGS =
[
  "-autosync 0 -mc 0.0",
  "",
  "-autosync 30 -mc 2.0",
]

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ VideoPlayer

Returns a new instance of VideoPlayer.



8
9
10
11
# File 'lib/video_player.rb', line 8

def initialize(file)
  @file = file
  @mplayer_arg = 0
end

Instance Method Details

#playObject



52
53
54
55
56
57
# File 'lib/video_player.rb', line 52

def play
  process_cmd('m')
  loop do
    wait_for_cmd
  end
end

#process_cmd(cmd) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/video_player.rb', line 20

def process_cmd(cmd)
  puts "process #{cmd}"
  case cmd
  when 'q' then exit!
  when 'r'
    @pid.kill!  if @pid
    @pid = spawn(@last_spawn)
  when 'm'
    @pid.kill!  if @pid
    @pid = spawn("mplayer -msglevel all=0 #{MP_ARGS[@mplayer_arg]} #{@file}")
    @mplayer_arg = (@mplayer_arg + 1)%MP_ARGS.size
  when 'v'
    @pid.kill!  if @pid
    @pid = spawn("vlc #{@file}")
  end
end

#spawn(cmd) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/video_player.rb', line 37

def spawn(cmd)
  @last_spawn = cmd
  pid = fork do
    exec(cmd)
  end
  
  o = Object.new
  (class <<o;self;end).send :define_method, :kill! do
    puts "killing #{pid}"
    ret = Process.kill 9, pid
  end
  
  o
end

#wait_for_cmdObject



13
14
15
16
17
18
# File 'lib/video_player.rb', line 13

def wait_for_cmd
  rdy = IO.select([STDIN])
  if rdy.first.include? STDIN
    process_cmd STDIN.getc.chr
  end
end