Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Hack:playJingle

From Music Player Daemon Community Wiki

Jump to: navigation, search
 

This script will start playing a jingle after the currently playing file. This can be used - for instance - to play a 'top of the hour' jingle.


playJingle.sh

#!/bin/bash
# MPD script to play a song (a jingle for instance) after the 
# currently playing song.
# The intention is to let this script get started by 
# a cronjob.
#
# Written by Marcel van der Schans based.
# Inspired by stop_after_current.sh by Harun Vos.

# Requires mpc - set the path here.
MPC=mpc
JINGLE="TopOfTheHour.mp3"

# Extract the interesting line from the mpc output.
# There must be a nicer way of dealing with multi-line shell variables.
function getDetailsLine()
{
  local d=$($MPC --format "no file name")
  d=$(tail -1 <<EOL
$(head -2 <<EOL2
$d
EOL2)
EOL)
  echo $d
}

# Get the playing/paused status.
function getStateFromLine()
{
  echo $*|cut -d' ' -f1
}

# Get the number of the currently playing song.
function getSongFromLine()
{
  echo $*|cut -d' ' -f2|cut -d'/' -f1|cut -d'#' -f2
}

# Get the number of the last song / the length of the playlist.
function getLastSongFromLine()
{
  echo $*|cut -d' ' -f2|cut -d' ' -f1|cut -d'/' -f2
}



# Dont do anything if we arent playing.
details=$(getDetailsLine)
currState=$(getStateFromLine $details)
if [ "$currState" != "[playing]" ]; then exit; fi

# Add jingle to (end of) playlist.
$MPC add $JINGLE

# Refresh our details line
details=$(getDetailsLine)

currSong=$(getSongFromLine $details)
lastSong=$(getLastSongFromLine $details)

# Move the last song (our jingle) to the next entry.
let nextSong=currSong+1
$MPC move $lastSong $nextSong


To let this script get started at a specific time (i.e. every hour), you can use crontab. Note that crontab uses the sh shell and doesn't load all your settings at the .profile file. To be able to run the playJingle.sh script - which is written for the bash shell - you can call the following (intermediate) script from crontab:


startPlayJingle.sh

#!/bin/sh
# Place the location of the playJingle.sh script below.
bash /home/username/playJingle.sh
Rate this article:
Share this article: