Class: MikePlayer::PlayThread
- Inherits:
-
Object
- Object
- MikePlayer::PlayThread
- Defined in:
- lib/mikeplayer/executable.rb,
lib/mikeplayer/play_thread.rb
Class Method Summary collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #elapsed ⇒ Object
- #info ⇒ Object
-
#initialize(volume: 1.0) ⇒ PlayThread
constructor
A new instance of PlayThread.
- #kill(signal) ⇒ Object
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #play(file) ⇒ Object
- #playing? ⇒ Boolean
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #to_json ⇒ Object
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
74 75 76 77 78 |
# File 'lib/mikeplayer/play_thread.rb', line 74 def self.alive?(pid) return system("ps -p #{pid} > /dev/null") unless pid.nil? false end |
.cmd_exist?(cmd) ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mikeplayer/play_thread.rb', line 80 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
52 53 54 |
# File 'lib/mikeplayer/play_thread.rb', line 52 def alive? MikePlayer::PlayThread.alive?(@pid) end |
#elapsed ⇒ Object
68 69 70 71 72 |
# File 'lib/mikeplayer/play_thread.rb', line 68 def elapsed return (@elapsed + (Time.now.to_i - @start_t)) if @start_t.positive? @elapsed end |
#info ⇒ Object
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
48 49 50 |
# File 'lib/mikeplayer/play_thread.rb', line 48 def kill(signal) Process.kill(signal, @pid) if alive? end |
#pause ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mikeplayer/play_thread.rb', line 35 def pause kill('INT') @elapsed += Time.now.to_i - @start_t @start_t = 0 sleep 0.2 kill('KILL') @paused = true end |
#paused? ⇒ Boolean
60 61 62 |
# File 'lib/mikeplayer/play_thread.rb', line 60 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
64 65 66 |
# File 'lib/mikeplayer/play_thread.rb', line 64 def alive? end |
#stop ⇒ Object
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
56 57 58 |
# File 'lib/mikeplayer/play_thread.rb', line 56 def stopped? false == alive? end |
#to_json ⇒ Object
23 24 25 |
# File 'lib/mikeplayer/executable.rb', line 23 def to_json return @filename end |