Class: MikePlayer::PlayThread

Inherits:
Object
  • Object
show all
Defined in:
lib/mikeplayer/executable.rb,
lib/mikeplayer/play_thread.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(volume: 1.0) ⇒ PlayThread

Returns a new instance of PlayThread.



3
4
5
6
# File 'lib/mikeplayer/executable.rb', line 3

def initialize(filename)
  @filename = filename
  @mp3info  = Mp3Info.new(filename)
end

Class Method Details

.alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/mikeplayer/play_thread.rb', line 80

def self.alive?(pid)
  return system("ps -p #{pid} > /dev/null") unless pid.nil?

  false
end

.cmd_exist?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mikeplayer/play_thread.rb', line 86

def self.cmd_exist?(cmd)
  if @test_cmd.nil?
    if system('command')
      @test_cmd = 'command -v'
    elsif system('bash -c "command"')
      @test_cmd = 'bash -c "command -v"'
    else
      raise "Missing 'command' command, which is used to test compatibility."
    end
  end

  if (true != system("#{@test_cmd} #{cmd} >/dev/null 2>&1"))
    return false
  end

  return true
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mikeplayer/play_thread.rb', line 58

def alive?
  MikePlayer::PlayThread.alive?(@pid)
end

#elapsedObject



74
75
76
77
78
# File 'lib/mikeplayer/play_thread.rb', line 74

def elapsed
  return (@elapsed + (Time.now.to_i - @start_t)) if @start_t.positive?

  @elapsed
end

#infoObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mikeplayer/executable.rb', line 8

def info
  artist = "#{@mp3info.tag.artist}"
  title  = "#{@mp3info.tag.title}"

  if (true == artist.empty?) && (true == title.empty?)
    return File.basename(filename, '.mp3')
  elsif (true == artist.empty?)
    artist = "?????"
  elsif (true == title.empty?)
    title  = "?????"
  end

  return "#{artist} - #{title}"
end

#kill(signal) ⇒ Object



52
53
54
55
56
# File 'lib/mikeplayer/play_thread.rb', line 52

def kill(signal)
  Process.kill(signal, @pid) if alive?
rescue Errno::ESRCH
  nil
end

#pauseObject



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

def pause
  kill('INT')

  @elapsed += Time.now.to_i - @start_t
  @start_t  = 0

  @wait_thread&.join(1)

  kill('KILL')

  @wait_thread&.join

  @paused = true
end

#paused?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mikeplayer/play_thread.rb', line 66

def paused?
  @paused && stopped?
end

#play(file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mikeplayer/play_thread.rb', line 14

def play(file)
  start_position = 0

  if paused?
    start_position = @elapsed
    @paused        = false
  else
    @elapsed = 0
  end

  start_thread(file: file, start_position: start_position)

  @start_t = Time.now.to_i
end

#playing?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/mikeplayer/play_thread.rb', line 70

def playing?
  alive?
end

#stopObject



29
30
31
32
33
34
35
# File 'lib/mikeplayer/play_thread.rb', line 29

def stop
  pause

  @elapsed = 0
  @start_t = 0
  @paused  = false
end

#stopped?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/mikeplayer/play_thread.rb', line 62

def stopped?
  false == alive?
end

#to_jsonObject



23
24
25
# File 'lib/mikeplayer/executable.rb', line 23

def to_json
  return @filename
end