We are in the process of migrating the Spacial Wiki content to our new Help Center at spacial.com.
Please visit the Help Center for latest Tips and Tricks, Documentation and Troubleshooting.
spacial.com/help-center

PAL: General StreamAds rotation

From SpacialAudio

Jump to: navigation, search

Question:

How can I insert advertisements provided by StreamAds into the queue without using the Clockwheel or Event scheduler?

Answer:

PAL scripting allows you greater control over when and how advertisements is added to the queue. The PAL script below is an example of you to insert StreamAds advertisements about every 20 minutes.

{*
 Description:
 This PAL script can be configured to insert StreamAds advertisements into the
 queue
 a) Every xx minutes, or
 b) Every xx songs that play
 
 This is done by editing the "WaitMinutes" and "WaitSongs" constants below.
 Please note ONE of these values must be zero as you generally do not want to wait
 xx minutes AND xx songs before playing advertisements.
 
 You can also change the value of "AdSpace" to configure how many seconds of
 advertisement space you want StreamAds to insert.
 
 Recommended values:
   AdSpace = 90;
   WaitMinutes = 20;
   WaitSongs = 0;
*}

//==============================================================================
// Custom settings - Please change these as required
//==============================================================================
const AdSpace = 90; //Advertisement space, in seconds

//NOTE: Only ONE of these needs to be set, the other one needs to be zero
const WaitMinutes = 20; //Play advertisements every xx minutes
const WaitSongs = 0; //Play advertisements every xx songs
//==============================================================================

var doWait    : boolean;
var songCount : Integer = 0;
var songInfo  : TSongInfo;

if (WaitMinutes<>0) AND (WaitSongs<>0) then
begin
  WriteLn('Warning: Set either WaitMinutes or WaitSongs, not BOTH!');
end;

//Insert advertisements
StreamAds['Providers (All)'].QueueTop(AdSpace);

//Wait for first Ad to start playing
//This will avoid LONG shows (60+ minutes) causing this script to
//queue a bunch of ads every 20 minutes
PAL.WaitForPlayCount(1);

//If WaitMinutes is set, wait for specified minutes
doWait := WaitMinutes>0;
while (doWait) do
begin
  WriteLn('Waiting for '+IntToStr(WaitMinutes)+' minutes:');
  PAL.WaitForTime(DateTime(Now+(WaitMinutes/(60*24))));
  doWait := false;
end;

//If WaitSongs is set, wait for specified number of tracks to play
//Note: We specifically do not count advertisements!
if (WaitSongs>0) then
  begin
    WriteLn('Waiting for '+IntToStr(WaitSongs)+' to play:');
    WriteLn('Note: Advertisements are not counted');
  end;
  
songCount := 0;
while (WaitSongs>songCount) do
begin
  //Check that track is not advertisement
  //Increase counter only if non-advertisement
  if (ActivePlayer <> nil) then
   begin
      songInfo := ActivePlayer.GetSongInfo();
     if(songInfo['songtype'] <> 'A') then
     begin
       songCount := songCount + 1;
       WriteStr(songCount); WriteStr(' ');
     end;
     songInfo.Free;
   end;
   
   //Only wait if needed
   doWait := true;
   while (doWait) and (WaitSongs>songCount) do
   begin
      PAL.WaitForPlayCount(1);
      doWait := false;
   end;
end;

//One we are done, start from top again
PAL.Loop := true;

Related links

Personal tools