Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Play random music

by Fingo (Monk)
on Mar 24, 2001 at 07:35 UTC ( #66823=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl 
   2: # Plays a random file from $dir using $player.  
   3: # I still have not figured a good way to quit other than killing
   4: # the perl program and then the player, anybody have some good ideas?
   5: # UPDATE: now properly replaces escape characters (old version replaced with space)
   6: use strict;
   7: my $dir = "/dosc/Music/";
   8: my $player ="mpg123";
   9: my ($i, @music);
  10: opendir (DIR, $dir);
  11: @music = readdir (DIR);
  12: closedir (DIR);
  13: foreach (@music) {
  14:     s/([\s|&|\\|\||(|)|'|_])/\\$1/g; # Add escape characters
  15: }
  16: srand();
  17: while (1)
  18: {
  19: $i = @music[int(rand(scalar(@music)))];
  20:     system "$player $dir$i";
  21: }

Replies are listed 'Best First'.
Re: Play random music
by methodius (Initiate) on Apr 08, 2001 at 10:32 UTC
    When I'm writing a daemon will drop a file ususally based on the PID $$ and then inside the loop test for the existence of the file and exit if the file is deleted:

    open PID,">$$" or die $@;
    close PID;

    while (-e $$) {

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlcraft [id://66823]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-10-24 09:46 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.