Basic shuffle MP3 player with controls


#1

Hi friends,
for a friend of mine’s band I try to install a „call-a-song“ hotline which playlist randomly one of their songs immediately after calling a special number.
I’m using Raspbx on a Raspberry Pi Zero W with the following (weird) Perl script:


#!/usr/bin/perl

use strict;
use warnings;
use Asterisk::AGI;

my $AGI = new Asterisk::AGI;

my $directory = "/var/lib/asterisk/playback/MP3";
my $volume = 2;
$AGI->exec('Set','VOLUME(TX)=' . $volume);

my $randomFile;
my @playQueue;
my $playQueueCounter = -1;
my $queueActive = 0;

sub RandomSongPath
{
  opendir(DIR, $directory);
  my @audioFiles = readdir(DIR);
  closedir(DIR);

  my $arrayLength = 0+@audioFiles;
  my $randomIndex = int(rand($arrayLength));
  my $randomFilename = @audioFiles[$randomIndex];
  my $fullsongpath = $directory . "/" . $randomFilename;
  @playQueue[$playQueueCounter+1] = $fullsongpath;
  return $fullsongpath;
}

sub PlayRandomSong
{
  $randomFile = RandomSongPath();
  $AGI->exec('MP3Player',$randomFile);
}

sub PlayPreviousSong
{
  $AGI->exec('MP3Player',@playQueue[$playQueueCounter-1]);
  $queueActive = 1;
  InitiateControls();
}

sub PlayNextSong
{
  $AGI->exec('MP3Player',@playQueue[$playQueueCounter+1]);
  InitiateControls();
}

sub InitiateControls
{
  #my $input = $AGI->wait_for_digit(-1);
  my $input = "";
  
  if ($AGI->wait_for_digit(-1) == '6')
  {
      PlayRandomSong();
      InitiateControls();
    /*
    if($queueActive == 1)
    {
      PlayNextSong();
      InitiateControls();
    }
    else
    {
      PlayRandomSong();
      $queueActive = 0;
      InitiateControls();
    }
    */
  } 
  elsif ($input == 4)
  {
    PlayPreviousSong();
    InitiateControls();
  } 
  elsif ($input == 2)
  {
    $AGI->exec('Set','VOLUME(TX)=' . $volume + 1);
    InitiateControls();
  } 
  elsif ($input == 8)
  {
    $AGI->exec('Set','VOLUME(TX)=' . $volume - 1);
    InitiateControls();
  }
}

PlayRandomSong();
InitiateControls();

Whole thing is working so far since I hear a random song at a call. But my “additional features” like skip, back, volume up and down are not working. After dialing - for example – 6, it beeps and music stops. And after dialing 6 again, PBX hangs up.

My dialplan is:

[fritzbox]
exten => raspbxuser,1,Answer()
same => n,Set(VOLUME(TX)=3)
same => n,AGI(/var/lib/asterisk/playback/pbxaudiobackend.pl)
;same => n,Hangup()

And the MP3Player cmd I use says:


“The callers can exit by dialing any digit.”
But how does this (huge) project manages to bypass the auto-hang-up by pressing a button?
https://issues.asterisk.org/jira/browse/ASTERISK-4373
Also found this:

Have no idea anymore :frowning:

May anybody help me?
Thanks!
Nico


#2

Using Asterisk rand() function and controlplayback() you could accomplish similar task