I have written a script for Elementary OS that pauses all running media players. This is because the sound may be really loud before suspension. Sadly, I can't get it to properly work.
#! /bin/sh
#
# Copyright (c) Jason Anderson 2014 <sirius-C at iname dot com>
# License: GPL-2
# * December 1, 2014
# - Version 0.1
# - Initial version
# * December 4, 2014
# - Version 0.2
# - Changed it for Totem video player
# - This is compatible with Audacious, Clementine, and VLC
# - Doesn't work for SMplayer
# * December 13, 2014
# - Cleaned up the code a little (added "suspend" to the case arguments).
# - Found out Banshee works, too.
# * December 14, 2014
# - Version 0.3
# - Refactored D-Bus code that sends the pause signal.
# Function for pausing the audio of each MPRIS-enabled media player.
pause_music() {
# Walk through the available media players via a call to qdbus and tell each of them
# to pause. Usually the user has just one media player going, but we don't know which
# one. So we have to do it this way.
for i in $(qdbus org.mpris.MediaPlayer2.*)
do
# Had a problem with Totem video player. This works for the currently running
# instance of Totem.
if [[ $i =~ "totem" ]]; then
# Don't know how this works for multiple instances of Totem. But then again, who
# has multiple versions of Totem running at once?
logger "[pause_media_players] Pausing Totem..."
totem --pause.
fi
logger "[pause_media_players] Pausing the media player..."
dbus-send --print-reply --dest=$i /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
logger "[pause_media_players] ...Okay, they've been paused."
done
}
logger "[13_pause-media-players] Started the pausing script."
# TODO: use another D-Bus application that's installed by default (i.e. gdbus).
# We need to use dbus-send so as to pause all of the media players that are listening via MPRIS.
case "$1" in
hibernate|suspend)
logger "[13_pause-media-players] We're hibernating. Going down!"
pause_music
;;
resume|thaw)
logger "[13_pause-media-players] We're unthawing. Rise and shine!"
pause_music
;;
# If we have anything else than a hibernate or a thaw, try to exit gracefully.
*)
exit $NA
;;
esac
exit 0
I thought it could be that when the computer comes back from suspension, it resumes the running media player. Turns out that's wrong, too. I also thought it was the order in which the scripts are executing. That wasn't the problem, either.
So I can't figure out whether it's just the syntax of the script, or it's something that has to do with D-Bus.