document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/bin/bash
  2. #Airplay monitor script v1.0 for use with MPD and shairport by James Hildebrandt 2013
  3. #Requires MPC be installed for command line control of MPD
  4. #Script checks to see if shairport has an active stream, if so it pauses MPD via MPC
  5. #Once Airplay stream ends it resumes MPD
  6. COUNTER=1
  7. AIRPLAY_STATE=0
  8. CURR_STATE=0
  9. PREV_STATE=0
  10. MPC_STATE=0
  11. #AIRPLAY_ON=""
  12. while [ $COUNTER == 1 ]; do
  13.         sleep 5
  14.         AIRPLAY_STATE=`netstat -t | grep rfe` #Check if anyone is playing music using airplay and set the output as variable
  15.         MPC_STATE=`mpc | head -2 | tail -1 | awk \'{print $1}\'` #Check the state of mpc to see if it is playing and set the output as variable
  16.         if [[ $AIRPLAY_STATE ]]; then #If AIRPLAY_STATE is not empty then someone is connected so ...
  17.                 echo on > /dev/shm/airplay_state #Write "on" to file airplay_state in ram
  18.                 CURR_STATE=1
  19.                 PREV_STATE=1
  20.         else
  21.                 echo off > /dev/shm/airplay_state #Otherwise write "off" to file airplay_state in ram
  22.                 CURR_STATE=0
  23.         fi
  24.         if [[ $AIRPLAY_STATE ]] && [ $MPC_STATE == "[playing]" ]; then #if someone is connected via airplay and mpc is playing
  25.                 mpc pause -q  #Then pause mpc and do it quietly so there is no output returned
  26.         fi
  27.         if [ $CURR_STATE == "0" ] && [ $MPC_STATE == "[paused]" ] && [ $PREV_STATE == "1" ]; then #If current airplay state is off, mpc is paused, and prev state is 1 then resume playing mpc
  28.                 mpc play -q
  29.                 PREV_STATE=0
  30.         fi
  31. done
');