Sha256: 242ebd7839bcf2729f2cbc3dbbaa9cf161c43edc9270e34e8f1ceceabc47e024

Contents?: true

Size: 824 Bytes

Versions: 1

Compression:

Stored size: 824 Bytes

Contents

module Mpg321
  class Playlist
    include Enumerable

    PLAYLIST_ADVANCE_EVENTS = [ :playback_finished, :file_not_found ]

    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

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

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

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

    private

    def dequeue
      @access.synchronize { @tracks.shift }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mpg321-1.0.0 lib/mpg321/playlist.rb