Class: Mpg321::Playlist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mpg321/playlist.rb

Constant Summary collapse

PLAYLIST_ADVANCE_EVENTS =
[ :playback_finished, :file_not_found ]

Instance Method Summary collapse

Constructor Details

#initialize(autoplay = false, client = Client.new) ⇒ Playlist

Returns a new instance of Playlist.



7
8
9
10
11
12
13
14
15
16
# File 'lib/mpg321/playlist.rb', line 7

def initialize autoplay = false, client = Client.new
  @tracks   = Array.new
  @access   = Mutex.new
  @autoplay = autoplay
  @client   = client

  PLAYLIST_ADVANCE_EVENTS.each do |event|
    @client.on(event) { advance }
  end
end

Instance Method Details

#advanceObject



23
24
25
26
27
28
29
# File 'lib/mpg321/playlist.rb', line 23

def advance
  if song = dequeue
    @client.play song
  else
    @client.stop if @client.loaded?
  end
end

#each(&block) ⇒ Object



31
32
33
# File 'lib/mpg321/playlist.rb', line 31

def each &block
  @access.synchronize { @tracks.each &block }
end

#enqueue(song) ⇒ Object



18
19
20
21
# File 'lib/mpg321/playlist.rb', line 18

def enqueue song
  @access.synchronize { @tracks << song }
  advance if @autoplay && !@client.loaded?
end