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)


84
85
86
87
88
# File 'lib/mikeplayer/play_thread.rb', line 84

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

  false
end

.cmd_exist?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mikeplayer/play_thread.rb', line 90

def self.cmd_exist?(cmd)
  if (true != system('command'))
    raise "Missing 'command' command, which is used to test compatibility."
  end

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

  return true
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


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

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

#elapsedObject



78
79
80
81
82
# File 'lib/mikeplayer/play_thread.rb', line 78

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



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

def kill(signal)
  Process.kill(signal, @pid) if alive?
end

#pauseObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mikeplayer/play_thread.rb', line 35

def pause
  kill('INT')

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

  10.times do
    break unless alive?

    sleep 0.1
  end

  kill('KILL')

  10.times do
    break unless alive?

    sleep 0.1
  end

  @paused = true
end

#paused?Boolean

Returns:

  • (Boolean)


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

def paused?
  @paused && stopped?
end

#play(file) ⇒ Object



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

def play(file)
  start_position = 0

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

  start_thread(file: file, start_position: start_position)

  @start_t = Time.now.to_i
end

#playing?Boolean

Returns:

  • (Boolean)


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

def playing?
  alive?
end

#stopObject



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

def stop
  pause

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

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  false == alive?
end

#to_jsonObject



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

def to_json
  return @filename
end