Quantcast
Channel: Squeezebox : Community : Forums
Viewing all articles
Browse latest Browse all 6235

Simple script for Radio News interruption for listening your music

$
0
0
Hello,

attached a simple Perl script, which plays your favoured Radio News Broadcast on a regular basis like every full hour. Your Music continues afterwards and plays like before. If you listen to a Internetstream already, the script will not interfere.

Main parts of the script are from the Webpage Domoticz https://www.domoticz.com/forum/viewtopic.php?t=15188:

The Perl script must be started via cron Deamon or Task Scheduler on Windows:

Code:

#!/usr/bin/perl -w
#
# radio.pl
#
# Guys: thanx for your work!

use IO::Socket;
use strict;
use Switch;

# --------- PLEASE SET YOUR PREFERENCES ---------#

# LMS Settings
my $serverAddress = '127.0.0.1';
my $serverPort = 9090;

my $szLine1                = "Radio Nachrichten";
my $szLine2                = "SR Info";
my $szSound                = "http://stream.srg-ssr.ch/drs4news/mp3_128.m3u";
my $iDuration = "20";
my $szSat = "0";
my $merker = "0";

my @iShuffle;
my @iIndex;
my @iPath;
my @iTime;
my @iTracks;
my @ihttp;
# Internal parameters
my $debug                = 0;        # 0 = off, 1 = on

my ($min, $hour) = (localtime(time))[1,2];
# print "MIN ,$min";
# print "Hour ,$hour";

  switch($hour) {
  case 8          { $szSound                = "http://192.168.178.4:8001/1:0:2:6F38:445:1:C00000:0:0:0:"; $szLine2 = "SWR Aktuell"; $iDuration = 300 }
  case 9            { $szSound                = "http://str0.creacast.com/sudtirol1a"; $szLine2 = "Südtirol 1"; $iDuration = 240 }
  case 10          { $szSound                = "http://opml.radiotime.com/Tune.ashx?id=s15051&partnerId=16&username=martwal"; $szLine2 = "BR 5"; $iDuration = 300 }
  case 11          { $szSound                = "http://opml.radiotime.com/Tune.ashx?id=s42828&partnerId=16&username=martwal"; $szLine2 = "DLF Nachrichten"; $iDuration = 300 }
  case 12          { $szSound                = "http://stream.srg-ssr.ch/drs4news/mp3_128.m3u"; $szLine2 = "SR Info"; $iDuration = 300 }
  case 13          { $szSound                = "http://www.wdr.de/wdrlive/media/wdr5.m3u"; $szLine2 = "WDR 5"; $iDuration = 300 }
  case 14          { $szSound                = "http://opml.radiotime.com/Tune.ashx?id=s176675&partnerId=16&username=martwal"; $szLine2 = "Hitradio Namibia"; $iDuration = 300 }
  case 15          { $szSound                = "http://streaming.brf.be/brf1-high.mp3"; $szLine2 = "Belgien 1"; $iDuration = 300 }
  case 16          { $szSound                = "http://opml.radiotime.com/Tune.ashx?id=s47781&partnerId=16&username=martwal"; $szLine2 = "News 89,4 Neuss"; $iDuration = 240 }
  case 17          { $szSound                = "http://opml.radiotime.com/Tune.ashx?id=s24885&partnerId=16&username=martwal"; $szLine2 = "NDR Info"; $iDuration = 240 }
  case 18          { $szSound                = "http://www.wdr.de/wdrlive/media/wdr5.m3u"; $szLine2 = "WDR 5"; $iDuration = 250 }
  case 19          { $szSound                = "http://www.wdr.de/wdrlive/media/wdr5.m3u"; $szLine2 = "WDR 5"; $iDuration = 300 }

  else              { $szSound                = "http://stream.srg-ssr.ch/drs4news/mp3_128.m3u"; $szLine2 = "SR Info"; $iDuration = 300 }
}
 $iDuration = "20" if ($debug eq 1) ;
#-------- NO USER SERVICABLE PARTS BELOW -------#

# Try to open a connection to the SLIMP3 CLI
my $LMS = IO::Socket::INET->new (PeerAddr => $serverAddress,
        PeerPort => $serverPort,
        Proto    => 'tcp',
        Timeout => 5,
        Type    => SOCK_STREAM)
                or die 'Couldn\'t connect to server';

    # Get the number of players
    my $playerCount = sendAndReceive('player count ?');
    $debug && print "$playerCount players found\n";

    for (my $i = 0; $i <  $playerCount; $i++) {
                          my $playerId = sendAndReceive("player id $i ?");
                        $ihttp[$i] = "0";
                          # Playermode
                          my $playerMode = sendAndReceive("$playerId mode ?");
                          $debug && print "$playerId playerMode: $playerMode\n";

                          if ($playerMode eq "play" ) {
                            #$debug && print "Mode\n";
                   
          # Get shuffle status (on, off)
          $iShuffle[$i] = sendAndReceive( "$playerId playlist shuffle ?");
          $debug && print "$playerId iShuffle: $iShuffle[$i]\n";

          # Get current song
          $iIndex[$i] = sendAndReceive( "$playerId playlist index ?");
          $debug && print "iIndex: $iIndex[$i]\n";
         
          # Get path and filename to current song
          $iPath[$i] = sendAndReceive( "$playerId playlist path $iIndex[$i] ?");
          $debug && print "iPath: $iPath[$i]\n";
         
          # Check if we are online (Radio Stream)
          $ihttp[$i] = "1" if( $iPath[$i] =~ /http/) || ( $iPath[$i] =~ /https/);
          $debug && print "ihttp: $ihttp[$i]\n";
         
          if( $ihttp[$i] eq "0"){
              # merken ob wir etwas zu tun haben
              $merker = "1";
             
              # Turn shuffle off
              sendAndReceive( "$playerId playlist shuffle 0");
             
              # Get the current position (in s)
              $iTime[$i] = sendAndReceive( "$playerId time ?");
              $debug && print "iTime: $iTime[$i]\n";
             
              # Stop the current song
              sendAndReceive( "$playerId stop");
             
              # Add the Radio Stream at the end of the playlist
              sendAndReceive( "$playerId playlist append $szSound");
              $debug && print "szSound: $szSound\n";
             
              # Get the number of tracks in the playlist
              $iTracks[$i] = sendAndReceive( "$playerId playlist tracks ?");
              $iTracks[$i] -= 1;
              $debug && print "iTracks: $iTracks[$i]\n";
              # Is now -1 if playlist was empty
              $iTracks[$i] = 0 if( $iTracks[$i] < 0);
             
              # Play the Radio stream
              sendAndReceive( "$playerId playlist index $iTracks[$i]");
             
              # Show the announcement message
              sendAndReceive( "$playerId display $szLine1 $szLine2 30");
          }
          }
          }
          if( $merker eq "1"){
         
          $debug && print "iDuration: $iDuration\n";
          sleep( $iDuration);
         
          for (my $i = 0; $i <  $playerCount; $i++) {
          my $playerId = sendAndReceive("player id $i ?");

          # Player Mode
          my $playerMode = sendAndReceive("$playerId mode ?");
          $debug && print "playerMode: $playerMode\n";
         
          $debug && print "ihttp: $ihttp[$i]\n";
          # Wiederherstellung des Startzustandes
          if ($playerMode eq "play" and $ihttp[$i] eq "0") {
 
              # Stop it
              sendAndReceive( "$playerId stop");
                   
              # Remove the radio stream from the playlist
              sendAndReceive( "$playerId playlist delete $iTracks[$i]");
              $debug && print "$playerId playlist delete $iTracks[$i]\n";
             
              # Reselect the old song
              sendAndReceive( "$playerId playlist index $iIndex[$i]");
              $debug && print "$playerId playlist index $iIndex[$i]\n";
             
              # Set the player in the old mode
              sendAndReceive( "$playerId play");
              sendAndReceive( "$playerId time $iTime[$i]");
               
              # Reinstate the shuffle mode
              sendAndReceive( "$playerId playlist shuffle $iShuffle[$i]");
                   
          }     
                }
    }
                close ($LMS);
    $debug && print "LMS Verbindung abgebaut\n";
   
  $debug && print "\n";
        # Send given cmd to $LMS and return answer with original command removed from
        # front if present.  Routine nicked from code by Felix Mueller. :-)
        sub sendAndReceive {
                my $cmd = shift;
                return if( $cmd eq "");
       
                print $LMS "$cmd\n";
                $debug > 1 && print "Sent $cmd to server\n";
                my $answer = <$LMS>;
                $debug > 1 && print "Server replied: $answer\n";
                $answer =~ s/$cmd //i;
                $answer =~ s/\n//;
       
                return $answer;
        }

Greetings

Viewing all articles
Browse latest Browse all 6235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>