Class: Somadic::Mplayer

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

Constant Summary collapse

MPLAYER =
'mplayer'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mplayer

Sets up a new instance of Mplayer.

Valid options are:

:cache     - how much memory in kBytes to use when precaching
             a file or URL
:cache_min - playback will start when the cache has been filled up
             to `cache_min` of the total.

See the mplayer man page for more.



19
20
21
22
23
24
# File 'lib/somadic/mplayer.rb', line 19

def initialize(options)
  @url = options[:url]
  @cache = options[:cache]
  @cache_min =options[:cache_min]
  @stopped = true
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/somadic/mplayer.rb', line 7

def cache
  @cache
end

#cache_minObject

Returns the value of attribute cache_min.



7
8
9
# File 'lib/somadic/mplayer.rb', line 7

def cache_min
  @cache_min
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/somadic/mplayer.rb', line 7

def url
  @url
end

Instance Method Details

#startObject

Starts mplayer on a new thread.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/somadic/mplayer.rb', line 27

def start
  @stopped = false
  @player_thread = Thread.new do
    cmd = command
    Somadic::Logger.debug("Mplayer#start: popen #{cmd}")
    pipe = IO.popen(cmd, 'r+')
    loop do
      line = pipe.readline.chomp
      if line['Starting playback']
        Somadic::Logger.debug("Mplayer#pipe: #{line}")
      elsif line.start_with?('ICY ')
        begin
          Somadic::Logger.debug("Mplayer#pipe: #{line}")
          _, v = line.split(';')[0].split('=')
          song = v[1..-2]
        rescue Exception => e
          Somadic::Logger.debug("unicode fuckup: #{e}")
        end
        notify(song)
      end
    end
    pipe.close
  end
rescue => e
  Somadic::Logger.error("Mplayer#start: error #{e}")
end

#stopObject

Stops mplayer.



55
56
57
58
59
# File 'lib/somadic/mplayer.rb', line 55

def stop
  Somadic::Logger.debug("Mplayer#stop")
  @stopped = true
  `killall mplayer`
end

#stopped?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/somadic/mplayer.rb', line 61

def stopped?
  @stopped
end