Class: Rubio::Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend = OS.linux? ? 'cvlc' : 'vlc -I rc') ⇒ Player

Returns a new instance of Player.



7
8
9
10
11
12
13
14
15
# File 'lib/rubio/player.rb', line 7

def initialize(backend = OS.linux? ? 'cvlc' : 'vlc -I rc')
  raise unless backend.is_a?(String)

  @backend = backend
  @pid = nil
  @thr = nil
  @status = []
  @history = []
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



5
6
7
# File 'lib/rubio/player.rb', line 5

def backend
  @backend
end

#historyObject

Returns the value of attribute history.



5
6
7
# File 'lib/rubio/player.rb', line 5

def history
  @history
end

#pidObject

Returns the value of attribute pid.



5
6
7
# File 'lib/rubio/player.rb', line 5

def pid
  @pid
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/rubio/player.rb', line 5

def status
  @status
end

#thrObject

Returns the value of attribute thr.



5
6
7
# File 'lib/rubio/player.rb', line 5

def thr
  @thr
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/rubio/player.rb', line 17

def alive?
  return false if @thr.nil?

  @thr.alive?
end

#play(url) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubio/player.rb', line 27

def play(url)
  # Do not include spaces in the command line
  # if a space exist :
  #   * sh -c command url # this process with @pid will be killed
  #   * cmmand url        # will not be killd because pid is differennt
  # if no space :
  #   * cmmand url        # will be killed by @pid
  raise if url.match(/\s/)

  @pid = spawn(*backend.split(' '), url)
  @thr = Process.detach(@pid)
  @status = [@pid, @thr]
  @history << @status
end

#stopObject



42
43
44
45
46
47
48
49
# File 'lib/rubio/player.rb', line 42

def stop
  return unless alive?

  r = Process.kill(OS.windows? ? :KILL : :TERM, pid)
  @thr = nil
  @pid = nil
  r
end

#stop?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rubio/player.rb', line 23

def stop?
  @thr.nil? || @thr.stop?
end

#stop_allObject



51
52
53
54
55
# File 'lib/rubio/player.rb', line 51

def stop_all
  @history.each do |pid, thr|
    Process.kill(OS.windows? ? :KILL : :TERM, pid) if thr.alive?
  end
end