dabreegster has asked for the wisdom of the Perl Monks concerning the following question:
An example script:package Audio::MP3::JMD; # Ewww! use Audio::Play::MPG123; use strict; use warnings; sub new { my $class = shift; my $args = shift; my @songs = @{$args->{songs}}; my $code = $args->{code}; my $self = { player => new Audio::Play::MPG123, queue => [@songs], pointer => 0, code => $code }; bless $self, $class; $self->{player}->load($self->{queue}->[$self->{pointer}]); $self->{player}->statfreq(1.0/$self->{player}->tpf); unless (fork()) { while (1) { until ($self->{player}->state == 0) { $self->{player}->poll(1); $self->{code}->($self->{player}); } $self->{pointer}++; unless ($self->{queue}->[$self->{pointer}]) { exit 1; } else { $self->{player}->load($self->{queue}->[$self->{pointer}]); } } } return $self; } sub add { my $self = shift; my @songs = @_; push @{$self->{queue}}, @songs; } sub next { my $self = shift; $self->{pointer}++; $self->stop() if $self->{pointer} > $#{$self->{queue}}; $self->{player}->load($self->{queue}->[$self->{pointer}]); } sub prev { my $self = shift; $self->{pointer}--; $self->stop() if ( $self->{pointer} < 0 ); $self->{player}->load($self->{queue}->[$self->{pointer}]); } sub pause { my $self = shift; $self->{player}->pause() unless $self->{player}->paused(); } sub resume { my $self = shift; $self->{player}->pause() if $self->{player}->paused(); } sub toggle { my $self = shift; $self->{player}->pause(); } sub restart { my $self = shift; $self->{player}->jump(0); } sub forward { my $self = shift; my $seconds = shift; $self->{player}->jump("+$seconds"); } sub backward { my $self = shift; my $seconds = shift; $self->{player}->jump("-$seconds"); } sub stop { print "STOP\n\n\n"; my $self = shift; $self->{player}->stop(); # The forked while loop doesn't catch this! # A brief note about exiting: When the user (program that uses this m +odule) # calls this method, stop(), or when the forked process that handles +the queue # reaches the end, the forked process will exit, as will the mpg123 p +rocess. # The user will then get a CHLD signal, which they will _HAVE TO HAND +LE_! } 1; __END__
#!/usr/bin/perl use Audio::MP3::JMD; use POSIX ":sys_wait_h"; $| = 1; my $player = new Audio::MP3::JMD ({ songs => ["/root/electronica/metro +idmodules.mp3"], code => sub { return; } }); $player->add("/root/electronica/fleetingecstacy.mp3"); $SIG{CHLD} = sub { my $stiff; while (($stiff = waitpid(-1, &WNOHANG)) > 0) { print "Nighty, night!\n"; print $stiff, "\n"; exit 1; } }; do { print join "\n", @{$player->{queue}}; print "\n"; print "-" x 80; sleep 5; } while 1;
Janitored by holli - added readmore-tag
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Music queue bugs
by northwind (Hermit) on May 26, 2005 at 20:40 UTC | |
by Tanktalus (Canon) on May 26, 2005 at 23:18 UTC | |
by dabreegster (Beadle) on May 27, 2005 at 02:21 UTC |