Class: Rubio::Model::Player
- Inherits:
-
Object
- Object
- Rubio::Model::Player
- Defined in:
- lib/rubio/model/player.rb
Constant Summary collapse
- CURRENTLY_PLAYING_NONE =
'None'
- CURRENTLY_PLAYING_LENGTH_PER_LINE =
90
Instance Attribute Summary collapse
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#currently_playing ⇒ Object
Returns the value of attribute currently_playing.
-
#history ⇒ Object
Returns the value of attribute history.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#show_currently_playing ⇒ Object
(also: #show_currently_playing?)
readonly
Returns the value of attribute show_currently_playing.
-
#status ⇒ Object
Returns the value of attribute status.
-
#thr ⇒ Object
Returns the value of attribute thr.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #continuously_fetch_currently_playing ⇒ Object
- #currently_playing_text ⇒ Object
- #info ⇒ Object
-
#initialize(backend = 'vlc -I rc', show_currently_playing: true) ⇒ Player
constructor
A new instance of Player.
- #play(url, station_name: 'N/A') ⇒ Object
- #stop(thr: nil, pid: nil, io: nil) ⇒ Object
- #stop? ⇒ Boolean
- #stop_all ⇒ Object
- #update_currently_playing ⇒ Object
Constructor Details
#initialize(backend = 'vlc -I rc', show_currently_playing: true) ⇒ Player
Returns a new instance of Player.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rubio/model/player.rb', line 13 def initialize(backend = 'vlc -I rc', show_currently_playing: true) raise unless backend.is_a?(String) @backend = backend @show_currently_playing = @pid = nil @thr = nil @status = {} @history = [] self. = CURRENTLY_PLAYING_NONE end |
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
9 10 11 |
# File 'lib/rubio/model/player.rb', line 9 def backend @backend end |
#currently_playing ⇒ Object
Returns the value of attribute currently_playing.
10 11 12 |
# File 'lib/rubio/model/player.rb', line 10 def @currently_playing end |
#history ⇒ Object
Returns the value of attribute history.
9 10 11 |
# File 'lib/rubio/model/player.rb', line 9 def history @history end |
#pid ⇒ Object
Returns the value of attribute pid.
9 10 11 |
# File 'lib/rubio/model/player.rb', line 9 def pid @pid end |
#show_currently_playing ⇒ Object (readonly) Also known as: show_currently_playing?
Returns the value of attribute show_currently_playing.
10 11 12 |
# File 'lib/rubio/model/player.rb', line 10 def @show_currently_playing end |
#status ⇒ Object
Returns the value of attribute status.
9 10 11 |
# File 'lib/rubio/model/player.rb', line 9 def status @status end |
#thr ⇒ Object
Returns the value of attribute thr.
9 10 11 |
# File 'lib/rubio/model/player.rb', line 9 def thr @thr end |
Instance Method Details
#alive? ⇒ Boolean
31 32 33 |
# File 'lib/rubio/model/player.rb', line 31 def alive? @thr&.alive? || (@io && !@io.closed?) end |
#continuously_fetch_currently_playing ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rubio/model/player.rb', line 63 def return if @continuously_fetching_currently_playing @continuously_fetching_currently_playing = true Glimmer::LibUI.timer(1) do @continuously_fetching_currently_playing end end |
#currently_playing_text ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/rubio/model/player.rb', line 80 def = info if && !.strip.empty? [@playing_station_name, ].join(' - ') else @playing_station_name end end |
#info ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/rubio/model/player.rb', line 89 def info result = io_command('info') = result.lines.find {|l| l.include?('now_playing:')} if .split('now_playing:').last.chomp.strip end rescue nil end |
#play(url, station_name: 'N/A') ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubio/model/player.rb', line 39 def play(url, station_name: 'N/A') # 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/) @playing_station_name = station_name if && backend == 'vlc -I rc' && !OS.windows? @io = IO.popen("#{backend} \"#{url}\"", 'r+') else @pid = spawn(*backend.split(' '), url) @thr = Process.detach(@pid) end @status = { thr: @thr, pid: @pid, io: @io } @history << @status end |
#stop(thr: nil, pid: nil, io: nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rubio/model/player.rb', line 99 def stop(thr: nil, pid: nil, io: nil) thr ||= @thr pid ||= @pid io ||= @io if thr&.alive? r = Process.kill(OS.windows? ? :KILL : :TERM, pid) elsif io && !io.closed? io.close r = io.closed? self. = CURRENTLY_PLAYING_NONE end thr = nil pid = nil io = nil r end |
#stop? ⇒ Boolean
35 36 37 |
# File 'lib/rubio/model/player.rb', line 35 def stop? @thr.nil? || @thr.stop? end |
#stop_all ⇒ Object
116 117 118 119 |
# File 'lib/rubio/model/player.rb', line 116 def stop_all @continuously_fetching_currently_playing = false @history.each { |history| stop(**history) } end |
#update_currently_playing ⇒ Object
74 75 76 77 78 |
# File 'lib/rubio/model/player.rb', line 74 def Glimmer::LibUI.queue_main do self. = if alive? end end |