Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

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.

share|improve this answer
add comment

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()
share|improve this answer
    
Instead of killing the process, you could also call the VLC quit DBus method by changing (bash) 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' –  JB0x2D1 Apr 12 at 13:05
add comment

After a fresh Ubuntu 12.04 install on a new HDD, this was solved for me by adding this ppa as described here. I guess the bug is fixed in VLC Player 2.1.3.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.