Class: Hearken::Player
- Inherits:
-
Object
show all
- Includes:
- Queue
- Defined in:
- lib/hearken/player.rb
Instance Attribute Summary collapse
Attributes included from Paths
#base_path, #index_path, #preferences_path, #queue_path
Instance Method Summary
collapse
Methods included from Queue
#dequeue, #each, #enqueue
Methods included from Paths
#create_paths, #in_base_dir, #in_queue_dir
Constructor Details
#initialize(preferences) ⇒ Player
Returns a new instance of Player.
14
15
16
17
18
19
20
21
22
|
# File 'lib/hearken/player.rb', line 14
def initialize preferences
@scrobbler = Scrobbler.new preferences
@scrobbler.enabled = true
@growl = Hearken::Notification::GrowlNotifier.new preferences
@notifiers = [@scrobbler, @growl]
@library = Library.new preferences
@library.reload
create_paths
end
|
Instance Attribute Details
#library ⇒ Object
Returns the value of attribute library.
12
13
14
|
# File 'lib/hearken/player.rb', line 12
def library
@library
end
|
#scrobbler ⇒ Object
Returns the value of attribute scrobbler.
12
13
14
|
# File 'lib/hearken/player.rb', line 12
def scrobbler
@scrobbler
end
|
Instance Method Details
#c(text, colour) ⇒ Object
24
25
26
|
# File 'lib/hearken/player.rb', line 24
def c text,colour
text.to_s.foreground colour
end
|
#current ⇒ Object
39
40
41
42
43
|
# File 'lib/hearken/player.rb', line 39
def current
in_base_dir do
(@pid and File.exist?('current_song')) ? YAML.load_file('current_song') : nil
end
end
|
#notify_finished(track) ⇒ Object
56
57
58
|
# File 'lib/hearken/player.rb', line 56
def notify_finished track
@notifiers.each {|notifier| notifier.finished track if notifier.respond_to? :finished}
end
|
#notify_started(track) ⇒ Object
52
53
54
|
# File 'lib/hearken/player.rb', line 52
def notify_started track
@notifiers.each {|notifier| notifier.started track if notifier.respond_to? :started}
end
|
#random_track ⇒ Object
64
65
66
|
# File 'lib/hearken/player.rb', line 64
def random_track
@library.row (rand * @library.count).to_i
end
|
#register(track) ⇒ Object
45
46
47
48
49
50
|
# File 'lib/hearken/player.rb', line 45
def register track
track.started = Time.now.to_i
in_base_dir do
File.open('current_song', 'w') {|f| f.print track.to_yaml }
end
end
|
#restart ⇒ Object
98
99
100
101
|
# File 'lib/hearken/player.rb', line 98
def restart
stop
start
end
|
#scrobbling(tf) ⇒ Object
60
61
62
|
# File 'lib/hearken/player.rb', line 60
def scrobbling tf
@scrobbler.enabled = tf
end
|
#start ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/hearken/player.rb', line 68
def start
return if @pid
@pid = fork do
player_pid = nil
Signal.trap('TERM') do
Process.kill 'TERM', player_pid if player_pid
exit
end
loop do
track = dequeue || random_track
next unless track
unless File.exist? track.path
puts "skipping track as #{track.path} does not exist"
next
end
notify_started track
register track
player_pid = track.path.escape2("\`").to_player
Process.wait player_pid
notify_finished track
end
end
end
|
#status ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/hearken/player.rb', line 28
def status
if @pid
track = self.current
puts "Since #{c Time.at(track.started), :cyan}\n\t#{track}"
played = Time.now.to_i-track.started
puts "#{c played, :yellow} seconds (#{c track.time.to_i-played, :yellow} remaining)" if track.time
else
puts 'not playing'.foreground(:yellow)
end
end
|
#stop ⇒ Object
92
93
94
95
96
|
# File 'lib/hearken/player.rb', line 92
def stop
return unless @pid
Process.kill 'TERM', @pid
@pid = nil
end
|