class MikePlayer::PlayThread
Public Class Methods
alive?(pid)
click to toggle source
# 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)
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 90 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
new(filename)
click to toggle source
# File lib/mikeplayer/executable.rb, line 3 def initialize(filename) @filename = filename @mp3info = Mp3Info.new(filename) end
Public Instance Methods
alive?()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 62 def alive? MikePlayer::PlayThread.alive?(@pid) end
elapsed()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 78 def elapsed return (@elapsed + (Time.now.to_i - @start_t)) if @start_t.positive? @elapsed end
info()
click to toggle source
# 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)
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 58 def kill(signal) Process.kill(signal, @pid) if alive? end
pause()
click to toggle source
# 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?()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 70 def paused? @paused && stopped? end
play(file)
click to toggle source
# 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?()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 74 def playing? alive? end
stop()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 27 def stop pause @elapsed = 0 @start_t = 0 @paused = false end
stopped?()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 66 def stopped? false == alive? end
to_json()
click to toggle source
# File lib/mikeplayer/executable.rb, line 23 def to_json return @filename end
Private Instance Methods
check_system()
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 136 def check_system %w[play].each do |cmd| raise "#{cmd} failed, do you have sox installed?" unless MikePlayer::PlayThread.cmd_exist?(cmd) end end
start_thread(file:, start_position: )
click to toggle source
# File lib/mikeplayer/play_thread.rb, line 109 def start_thread(file:, start_position: ) args = [ 'play', '--no-show-progress', '--volume', @volume.to_s, file, 'trim', start_position.to_s, ] stdin, stdother, thread_info = Open3.popen2e(*args) @pid = thread_info.pid 10.times do break if alive? sleep 0.2 end raise "Failed to play #{stdother.read}" unless alive? stdin.close stdother.close self end