Class: MPD::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-mpd/playlist.rb

Overview

An object representing an .m3u playlist stored by MPD.

Playlists are stored inside the configured playlist directory. They are addressed with their file name (without the directory and without the .m3u suffix).

Some of the commands described in this section can be used to run playlist plugins instead of the hard-coded simple m3u parser. They can access playlists in the music directory (relative path including the suffix) or remote playlists (absolute URI with a supported scheme).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mpd, options) ⇒ Playlist

Returns a new instance of Playlist.



18
19
20
21
22
# File 'lib/ruby-mpd/playlist.rb', line 18

def initialize(mpd, options)
  @name = options.is_a?(Hash) ? options[:playlist].to_s : options.to_s # convert to_s in case the parser converted to int
  @mpd = mpd
  #@last_modified = options[:'last-modified']
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/ruby-mpd/playlist.rb', line 16

def name
  @name
end

Instance Method Details

#add(uri) ⇒ Boolean

Adds URI to the playlist.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



50
51
52
# File 'lib/ruby-mpd/playlist.rb', line 50

def add(uri)
  @mpd.send_command :playlistadd, @name, uri
end

#clearBoolean

Clears the playlist.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



68
69
70
# File 'lib/ruby-mpd/playlist.rb', line 68

def clear
  @mpd.send_command :playlistclear, @name
end

#delete(pos) ⇒ Boolean

Deletes song at position POS from the playlist.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



74
75
76
# File 'lib/ruby-mpd/playlist.rb', line 74

def delete(pos)
  @mpd.send_command :playlistdelete, @name, pos
end

#destroyBoolean

Deletes the playlist from the disk.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



93
94
95
# File 'lib/ruby-mpd/playlist.rb', line 93

def destroy
  @mpd.send_command :rm, @name
end

#load(range = nil) ⇒ Boolean

Loads the playlist into the current queue. Playlist plugins are supported.

Since 0.17, a range can be passed to load, to load only a part of the playlist.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



44
45
46
# File 'lib/ruby-mpd/playlist.rb', line 44

def load(range=nil)
  @mpd.send_command :load, @name, range
end

#move(songid, songpos) ⇒ Boolean

Moves song with SONGID in the playlist to the position SONGPOS.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



80
81
82
# File 'lib/ruby-mpd/playlist.rb', line 80

def move(songid, songpos)
  @mpd.send_command :playlistmove, @name
end

#rename(new_name) ⇒ Boolean

Renames the playlist to new_name.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



86
87
88
89
# File 'lib/ruby-mpd/playlist.rb', line 86

def rename(new_name)
  @mpd.send_command :rename, @name, new_name
  @name = new_name
end

#searchadd(type, what) ⇒ Boolean

Searches for any song that contains what in the type field and immediately adds them to the playlist. Searches are NOT case sensitive.

Parameters:

  • type (Symbol)

    Can be any tag supported by MPD, or one of the two special parameters: :file to search by full path (relative to database root), and :any to match against all available tags.

Returns:

  • (Boolean)

    returns true if successful.

Raises:

  • (MPDError)

    if the command failed.



62
63
64
# File 'lib/ruby-mpd/playlist.rb', line 62

def searchadd(type, what)
  @mpd.send_command :searchaddpl, @name, type, what
end

#songsArray<MPD::Song>

Lists the songs in the playlist. Playlist plugins are supported.

Returns:

  • (Array<MPD::Song>)

    songs in the playlist.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-mpd/playlist.rb', line 26

def songs
  result = @mpd.send_command(:listplaylistinfo, @name)
  if result.to_s =~ URI::regexp
    Song.new({:file => result, :time => 0})
  else
    result.map {|hash| Song.new(hash) }
  end
rescue TypeError
  puts "Files inside Playlist '#{@name}' do not exist!"
  return nil
rescue NotFound
  return [] # we rescue in the case the playlist doesn't exist.
end