Audio-Daemon

 view release on metacpan or  search on metacpan

Daemon/MPG123.pm  view on Meta::CPAN

package Audio::Daemon::MPG123;

use strict;
use IO::Socket;
use IO::Select;
use Audio::Daemon;

use vars qw(@ISA $VERSION $HAVEPLAY $MIXER);

BEGIN { 
  eval "use Audio::Play::MPG123";
  $HAVEPLAY = $@?0:1;
  eval "use Audio::Mixer";
  $MIXER = $@?0:1;
}

@ISA = qw(Audio::Daemon);
$VERSION='0.99Beta';

sub new {
  my $class = shift;
  die "Cannot find Audio::Play::MPG123" if (! $HAVEPLAY);
  my $self = $class->SUPER::new(@_);
  bless $self, $class;
  # initilize current playlist
  $self->{playlist} = [];
  # initialize random index.
  $self->{random} = [];
  $self->{revrandom} = [];
  # initilize my current states of various things
  $self->{state} = {random => 0, repeat => 0, state => 0};
  return $self;
}

sub player {
  my $self = shift;
  $self->{player} = shift if (scalar @_);
  return $self->{player};
}

sub stop {
  my $self = shift;
  my $player = $self->player;
  $self->{state}{state} = 0;
  $player->stop;
}
  
sub play {
  my $self = shift;
  my $remote = shift;
  my $player = $self->player;
  if (defined $remote && ref $remote && scalar @{$remote->{args}}) {
    if ($self->{state}{random}) {
      $self->{state}{renid} = $self->{revrandom}[$remote->{args}[0]];
      $self->{state}{regid} = $remote->{args}[0];
    }
  } elsif ($player->state == 1) {
    $player->pause; # toggles pause flag (starts playing)
    return;
  }
  $self->debug("Random is ".($self->{state}{random})?'true':'false');
  my $id = $self->{state}{regid};
  $self->info("Setting current playlist id to $id and song to ".$self->{playlist}[$id]);
  if (! -r $self->{playlist}[$id] && ($self->{playlist}[$id]!~/^http/)) {
    $self->crit("File Not Found or Not Readable: ".$self->{playlist}[$id]);
    return;
  }
  my $retval = eval { $player->load($self->{playlist}[$id]); };
  $self->crit($@) if ($@);
  $self->info("load returned $retval");
}

sub pause {
  my $self = shift;
  my $player = $self->player;
  $player->pause;
}

sub add {
  my $self = shift;
  my $remote = shift;
  push @{$self->{playlist}}, @{$remote->{args}};
  # create array of indexes: 



( run in 1.971 second using v1.01-cache-2.11-cpan-a9496e3eb41 )