Class: Mplayer

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/terminal_player/mplayer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mplayer

Returns a new instance of Mplayer.



4
5
6
7
# File 'lib/terminal_player/mplayer.rb', line 4

def initialize(options)
  @options = options
  # assumes options has :cache, :cache_min, :url
end

Instance Method Details

#notify(message) ⇒ Object



39
40
41
42
# File 'lib/terminal_player/mplayer.rb', line 39

def notify(message)
  changed
  notify_observers(Time.now, message)
end

#playObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/terminal_player/mplayer.rb', line 9

def play
  return if @options[:stub] # HACK

  @player_thread = Thread.new do
    player = "mplayer -quiet -cache #{@options[:cache]} " \
             "-cache-min #{@options[:cache_min]} " \
             "-playlist \"#{@options[:url]}\" 2>&1"
    notify "starting player (cache #{@options[:cache]}, min #{@options[:cache_min]})..."
    @player_pipe = IO.popen(player, "r+")
    loop do
      line = @player_pipe.readline.chomp
      if line['Starting playback']
        notify line
      elsif line['ICY']
        notify line
      elsif line['Cache empty']
        notify line
      elsif line['Cache fill']
        notify "filling cache..."
      end
    end
    @player_pipe.close
  end
  @player_thread.join
end

#write(char) ⇒ Object



35
36
37
# File 'lib/terminal_player/mplayer.rb', line 35

def write(char)
  @player_pipe.write(char)
end