Class: Player
- Inherits:
-
Object
- Object
- Player
- Defined in:
- lib/mdisc/player.rb
Overview
FIXME: There is something wrong…
Constant Summary collapse
- WAIT_TIME =
0.5
Instance Attribute Summary collapse
-
#ui ⇒ Object
Returns the value of attribute ui.
Instance Method Summary collapse
-
#initialize ⇒ Player
constructor
A new instance of Player.
- #next ⇒ Object
- #pause ⇒ Object
- #play(datatype, songs, idx, switch_flag = false) ⇒ Object
- #prev ⇒ Object
- #recall ⇒ Object
- #resume ⇒ Object
- #stop ⇒ Object
- #switch ⇒ Object
Constructor Details
#initialize ⇒ Player
Returns a new instance of Player.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mdisc/player.rb', line 10 def initialize self.ui = Ui.new @datatype = 'songs' @mpg123_thread = nil @mpg123_pid = nil = false @pause_flag = false @songs = [] @idx = 0 @carousel = ->(left, right, x){x < left ? right : (x > right ? left : x)} end |
Instance Attribute Details
#ui ⇒ Object
Returns the value of attribute ui.
8 9 10 |
# File 'lib/mdisc/player.rb', line 8 def ui @ui end |
Instance Method Details
#next ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/mdisc/player.rb', line 89 def next stop sleep WAIT_TIME @idx = @carousel[0, @songs.size - 1, @idx + 1] recall end |
#pause ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/mdisc/player.rb', line 71 def pause @pause_flag = true # send SIGSTOP to pipe Process.kill(:SIGSTOP, @mp3id) item = @songs[@idx] ui.(item['song_name'], item['artist'], true) end |
#play(datatype, songs, idx, switch_flag = false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mdisc/player.rb', line 42 def play(datatype, songs, idx, switch_flag = false) @datatype = datatype if !switch_flag @pause_flag ? resume : pause if elsif switch_flag @songs = songs @idx = idx ? switch : recall end end |
#prev ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/mdisc/player.rb', line 97 def prev stop sleep WAIT_TIME @idx = @carousel[0, @songs.size - 1, @idx - 1] recall end |
#recall ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mdisc/player.rb', line 22 def recall = true @pause_flag = false item = @songs[@idx] ui.(item['song_name'], item['artist']) @thread = Thread.new do # Add input option: --continue, # play a song continuously in case the network becomes offline occasionally. @mp3id, stdin, stdout, stderr = Open4::popen4('mpg123', '--continue', item['mp3_url']) Process::waitpid2 @mp3id if @idx = @carousel[0, @songs.size - 1, @idx + 1] recall end end end |
#resume ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/mdisc/player.rb', line 80 def resume @pause_flag = false # send SIGCONT to pipe Process.kill(:SIGCONT, @mp3id) item = @songs[@idx] ui.(item['song_name'], item['artist']) end |
#stop ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mdisc/player.rb', line 60 def stop return unless return unless @thread return unless @mp3id = false # kill this process and thread Process.kill(:SIGKILL, @mp3id) Thread.kill @thread end |
#switch ⇒ Object
54 55 56 57 58 |
# File 'lib/mdisc/player.rb', line 54 def switch stop sleep WAIT_TIME recall end |