When VLC is running, it prevents the computer from suspending.
I want it to allow suspend IF VLC is stopped (the media finished playing).
How can this be done?
I was experiencing the same issue on a Ubuntu 12.04 PC. I fixed it by taking @hbdgaf's suggestion. Sucks to fall asleep watching something and wake up to find out that the screen saver never came on. My solution written in C is what I use now. It is a daemon that uses the low level DBus API to call methods on VLC to get the playback status and ask VLC to quit if it is stopped. The bash and python scripts were not reliable when run as Ubuntu Startup Applications on my machine, which is what I wanted. I shouldn't have to remember to restart a script every time I reboot, right? They would work if run manually, however. The C program doesn't have that issue. Stick it in Startup Applications and forget about it.
I'm told that the DBus object name is different in Ubuntu 14.04 (but VLC has the same issue with screen saver). org.mpris.MediaPlayer2.vlc.instanceXXXXX
on 14.04. org.mpris.MediaPlayer2.vlc-XXXXX
on 12.04. Thanks Sneetsher.
Bash solution using GDBus (was installed by default on my Ubuntu 12.04):
#VLC Watchdog Bash Script (vlcwd.sh)
while [ 1 -eq 1 ]
do
if [ "$(pgrep vlc)" != "" ] #if VLC is running
then #get the playback status and save to variable pbs
pbs=$(bash -c 'gdbus call --session \
--dest org.mpris.MediaPlayer2.vlc-$(pgrep vlc) \
--object-path /org/mpris/MediaPlayer2 \
--method org.freedesktop.DBus.Properties.Get \
"org.mpris.MediaPlayer2.Player" \
"PlaybackStatus"')
if [ "$pbs" = "(<'Stopped'>,)" ] #if VLC is stopped
then kill -9 $(pgrep vlc) #then kill it so it doesn't block my screen saver
fi
fi
sleep 5
done
Python
#vlc_watchdog_daemon.py
import time
time.sleep(30)
import dbus
import os
import subprocess
from subprocess import Popen, PIPE
import daemon
def vlc_killer():
bus = dbus.SessionBus()
vlc_media_player_obj = bus.get_object("org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2")
props_iface = dbus.Interface(vlc_media_player_obj, 'org.freedesktop.DBus.Properties')
pb_stat = props_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
if pb_stat == 'Stopped':
os.system("kill -9 $(pidof vlc)")
def vlc_is_running():
ps = subprocess.Popen(['ps', '-e'], stdout = PIPE)
out, err = ps.communicate()
for line in out.splitlines():
if 'vlc' in line:
return True
return False
def run():
while True:
if vlc_is_running():
vlc_killer()
else:
time.sleep(5)
with daemon.DaemonContext():
run()
then kill -9 $(pgrep vlc)
to then bash -c 'gdbus call --session --dest org.mpris.MediaPlayer2.vlc-$(pgrep vlc) --object-path /org/mpris/MediaPlayer2 --method org.mpris.MediaPlayer2.Quit'
You could set up a DBus query to get the play state of VLC and kill the process when it's done playing. That should release its hold on suspend state.