- 0 Talk
-
Ralbum.pl
Perl script that adds a random album to the playlist or clears the playlist and play a random album. Can also monitor the playlist and add a random album when the last song in the playlist is reached.
#!/usr/bin/perl
# Basic, inelegant script to add a random album to the MPD playlist.
# Invoked without any arguments, it will simply add a random album to the
# end of the playlist.
# -c will clear the existing playlist and play a random album.
# -m will monitor for the position in the playlist and add a random album
# if the last song in the playlist is playing.
use strict;
use warnings;
use utf8;
use Encode;
use POSIX qw(locale_h);
use Audio::MPD;
my $index;
my $song;
my $cur_song;
my $now_play;
my $match;
my $mpd = Audio::MPD->new(
#--------------------------------------------------
# Modify to your needs
# hostname => "",
# port => "",
# password => "",
#--------------------------------------------------
);
srand(time() ^($$ + ($$ <<15))) ;
sub get_songs {
my @b = ();
my @c = ();
my @albums = $mpd->collection->all_albums;
push(@b,@albums);
$index = rand @b;
my @songs = $mpd->collection->songs_from_album($b[$index]);
push(@c,@songs);
return @c;
}
sub random_album {
my @random_album = &get_songs;
foreach $song (@random_album) {
$mpd->playlist->add($song->file());
}
}
if (@ARGV) {
if ($ARGV[0] eq "-c") {
$mpd->stop;
$mpd->playlist->clear;
random_album;
$mpd->play;
exit;
}
if ($ARGV[0] eq "-m") {
while (1){
if ($mpd->status->state ne "stop" && $mpd->song->pos == ($mpd->status->playlistlength - 1)) {
random_album;
}
sleep 10;
}
}
}
else {
random_album;
}