Class: Ektoplayer::Operations::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/ektoplayer/operations/playlist.rb

Instance Method Summary collapse

Constructor Details

#initialize(operations, playlist, player, trackloader) ⇒ Playlist

Returns a new instance of Playlist.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/ektoplayer/operations/playlist.rb', line 4

def initialize(operations, playlist, player, trackloader)
   @playlist, @player, @trackloader = playlist, player, trackloader
   register = operations.with_register('playlist.')

   register.(:clear,   &@playlist.method(:clear))
   register.(:delete,  &@playlist.method(:delete))

   %w(play reload play_next play_prev download_album).each do |operation|
      register.(operation, &self.method(operation))
   end
end

Instance Method Details

#download_album(index) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ektoplayer/operations/playlist.rb', line 16

def download_album(index)
   return unless track = @playlist[index]
   Thread.new do
      @trackloader.download_album(track['url']) rescue (
         Application.log(self, $!)
      )
   end.join(0.3) # prevent too many hits
end

#play(index) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ektoplayer/operations/playlist.rb', line 34

def play(index)
   return unless track = @playlist[index]
   @playlist.current_playing=(index)
   Thread.new do
      @player.play(@trackloader.get_track_file(track['url'], http_okay: @player.can_http?))
   end.join(0.3) # prevent too many hits
end

#play_nextObject



42
43
44
45
46
# File 'lib/ektoplayer/operations/playlist.rb', line 42

def play_next
   return if @playlist.empty? or !@playlist.current_playing 
   index = (@playlist.current_playing + 1) % @playlist.size
   play(index)
end

#play_prevObject



48
49
50
51
52
53
54
55
56
# File 'lib/ektoplayer/operations/playlist.rb', line 48

def play_prev
   return if @playlist.empty? or !@playlist.current_playing 

   if @playlist.current_playing == 0
      play(@playlist.size - 1)
   else
      play(@playlist.current_playing - 1)
   end
end

#reload(index) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/ektoplayer/operations/playlist.rb', line 25

def reload(index)
   return unless track = @playlist[index]
   Thread.new do
      @trackloader.get_track_file(track['url'], reload: true) rescue (
         Application.log(self, $!)
      )
   end.join(0.3) # prevent too many hits
end