Class: AmpachePlaylist
- Inherits:
-
Object
- Object
- AmpachePlaylist
- Defined in:
- lib/lib-classes.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
Instance Method Summary collapse
- #add(song) ⇒ Object
- #command(cmd, match = //) ⇒ Object
- #console ⇒ Object
-
#get(value) ⇒ Object
I borrowed these two methods from the author of mplayer-ruby! so my thanks to Artuh Chiu and his great gem mplayer-ruby.
-
#initialize ⇒ AmpachePlaylist
constructor
A new instance of AmpachePlaylist.
- #mplayer_start(song) ⇒ Object
- #next ⇒ Object
- #now_playing ⇒ Object
- #pause ⇒ Object
- #started! ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
Constructor Details
#initialize ⇒ AmpachePlaylist
Returns a new instance of AmpachePlaylist.
71 72 73 74 |
# File 'lib/lib-classes.rb', line 71 def initialize @started = false @list = [] end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
76 77 78 |
# File 'lib/lib-classes.rb', line 76 def list @list end |
Instance Method Details
#add(song) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/lib-classes.rb', line 115 def add(song) say(HighLine.new.color("adding song ",:green) + "#{song.title}") if !@pid && !started? mplayer_start(song) else @list << song end end |
#command(cmd, match = //) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/lib-classes.rb', line 189 def command(cmd, match = //) @stdin.puts(cmd) response = "" begin t = Timeout::timeout(3) do until response =~ match response = @stdout.gets #puts response #XXX escaping bad utf8 chars ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') if response response =ic.iconv(response + ' ')[0..-2] else response == '' end end end return response.gsub("\e[A\r\e[K", "") rescue Timeout::Error return end end |
#console ⇒ Object
109 110 111 112 113 |
# File 'lib/lib-classes.rb', line 109 def console while ((cmd = gets.chomp) != 'exit' ) @stdin.puts cmd end end |
#get(value) ⇒ Object
I borrowed these two methods from the author of mplayer-ruby! so my thanks to Artuh Chiu and his great gem mplayer-ruby
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/lib-classes.rb', line 173 def get(value) field = value.to_s match = case field when "time_pos" then "ANS_TIME_POSITION" when "time_length" then "ANS_LENGTH" when "file_name" then "ANS_FILENAME" else "ANS_#{field.upcase}" end res = command("get_#{value}", /#{match}/) res.gsub("#{match}=", "").gsub("'", "") unless res.nil? end |
#mplayer_start(song) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lib-classes.rb', line 87 def mplayer_start(song) #puts song.inspect $options[:path] ||= '/usr/bin/mplayer' $options[:timeout] ||= 15 = "-slave -quiet" mplayer = "#{$options[:path]} #{} \"#{song.url}\"" @pid, @stdin, @stdout, @stderr = Open4.popen4(mplayer) #puts mplayer started! Thread.new do ignored, status = Process::waitpid2 @pid next_song = @list.slice!(0) if next_song && started? mplayer_start(next_song) else puts "PLAYLIST OVER" @started = false @pid = nil end end end |
#next ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/lib-classes.rb', line 143 def next begin # quit current mplayer instance.. thread will fire up next song! @stdin.puts "quit" if @pid && started? rescue Errno::EPIPE => e puts "playlist is over on next" @pid = nil end end |
#now_playing ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/lib-classes.rb', line 153 def return "Not playing man!" unless started? && !@pid.nil? begin s = get("meta_title") return "data not available OR playlist is over" if s.nil? s+= get("meta_artist") rescue s s+= get("meta_album") rescue s s.chomp! return s rescue Errno::EPIPE => e @pid = nil @started = false return "playlist is over here" #, or you got an ERROR from mplayer: #{e.to_s}" end end |
#pause ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/lib-classes.rb', line 135 def pause begin @stdin.puts "pause" if @pid && started? rescue Errno::EPIPE puts "playlist is over" end end |
#started! ⇒ Object
82 83 84 |
# File 'lib/lib-classes.rb', line 82 def started! @started = true end |
#started? ⇒ Boolean
78 79 80 |
# File 'lib/lib-classes.rb', line 78 def started? @started == true end |
#stop ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/lib-classes.rb', line 124 def stop begin @started = false @list = [] @stdin.puts "quit" if @pid rescue Errno::EPIPE puts "playlist is over" end @pid = nil end |