Hack:playMPCRandomAd
Talk0
542pages on
this wiki
this wiki
Just another hack for MPC (from MPD) to play a set of MP3's listed in a script (inside MPD DB) written by Rodrigo Martínez (roroMarti), based from playJingle.sh written by Marcel van der Schans and his inspiration stop_after_current.sh by Harun Vos
#!/bin/bash
#################################################################################################################
# playMPCRandomAd.sh #
# Just another hack for MPC (from MPD) to random play a set of MP3's listed in a script (inside MPD DB) #
# #
# Try to put this script calling from a scheduler (cron) in order tu execute in a lapse time each ie 10 minutes #
# Write by Rodrigo Martínez (roro Marti) #
# #
# Writen based on playJingle by Marcel van der Shans, and this inspired in #
# stop_after_current.sh by Harun Vos. #
# ###############################################################################################################
########## VARIABLES SECTION ############
# Requires mpc (Music player Control) - set the path here.
MPC=mpc
JINGLE=""
# Insert here in this list (Mp3PLayList) the mp3's that you need random for.
# This mp3's needs be inside the MPD Database structure
# You could test if the mp3 exist in this database executing in a linux console as follow:
# >_ mpc add yourmp3file.mp3 (this command add at the last of the queue play list the song yourmp3file.mp3)
# if this command is
# good: mpc answer >_ "<empty answer>" = ""
# bad : mpc answer >_ "error: directory or file not found"
Mp3PlayList="ea1.mp3
ea2.mp3
ea3.mp3"
########## FUNCTION's SECTION ############
# Extract the interesting line from the mpc output.
# There must be a nicer way of dealing with multi-line shell variables.
function getDetailsLine()
{
mpc > temporal.tmp
d=$(cat temporal.tmp | head -2 temporal.tmp | tail -1)
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
}
# let god say wich mp3 to play.
function randomJingle()
{
RANDOM=$$ # Reseed the random number generator using script process ID.
mp3PlayList=($Mp3PlayList) # Read into array variable.
mp3Elements=${#mp3PlayList[*]} # Count how many elements.
# echo ${mp3PlayList[$((RANDOM%mp3Elements))]} # Debug line
echo ${mp3PlayList[$((RANDOM%mp3Elements))]}
}
###### PROCEDURE SECTION #######
# Dont do anything if we arent playing.
details=$(getDetailsLine)
currState=$(getStateFromLine $details)
if [ "$currState" != "[playing]" ]; then exit; fi
# Add jingle to (end of) playlist.
#JINGLE =$(randomJingle)
$MPC add $(randomJingle)
# 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
########## EOF #########################