Announcement

Collapse
No announcement yet.

Don't sleep while playing audio

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] Don't sleep while playing audio

    As the title suggests, I wanted something very simple: Kubuntu / KDE Plasma shouldn't sleep while audio is playing. There are apparently script workarounds for Mint and Ubuntu using Gnome, but I couldn't find anything for KDE. Specifically, my need is this: A) If audio is playing, then don't sleep; B) but do please save power by turning the screen off; and C) allow the screen to lock for security reasons. My solution after reading the following references is below, but it's not exactly perfect (also, while my instructions are for Kubuntu, I assume some tweaks would make this solution suitable for any of the _ubuntus and probably other distros). I discuss this solution and ask questions at the end. Also, I apologize if I am posting in the wrong place or have broken some rule here; I hope this thread helps other users with my specific needs.

    Reference:
    https://ubuntuforums.org/showthread.php?t=2232064
    https://unix.stackexchange.com/quest...rently-playing
    https://stackoverflow.com/questions/...04952#17404952
    https://bbs.archlinux.org/viewtopic.php?id=204253

    Solution:
    1. Install xdotool: sudo apt-get install xdotool
    2. Create script: Copy and paste code below into text file >> save as name_this_script.sh wherever you keep your scripts.
    3. Make script executable: Righ-click > Properties > Permissions >> Check 'Is Executable')
    4. Change 'Lock Screen' setting to be LESS THAN the 'sleep 11m' (see code below): System Settings >> Workspace Behavior >> Screen Locking >> Tick 'Automatically Lock Screen after [(t<11) minutes]
    5. Change 'Dim Screen' and 'Screen Energy Saving' settings to be LESS THAN the 'Lock Screen': System Settings >> Power Management >> Energy Saver > On AC Power/On Battery >> Tick 'Dim screen After [(t<10) minutes]' and 'Screen Energy Saving Switch off after [(t<9) minutes]'
    6. Change 'Suspend session' to be MORE THAN 'sleep 11m': System Settings > Power Management >> Energy Saver >> On AC Power/On Battery/On Low Battery > Tick 'Suspend session After [(t>11) minutes]'
    7. Run script on startup: System Settings >> Startup and Shutdown >> Autostart >> Add Script


    Note: Of course you can change the time variables to anything you want, just make sure to stick to the ordering: t 'Dim Screen' < t 'Screen Energy Saving' < t 'Lock Screen' < t 'sleep' (in script) < t 'Suspend session'. Also, make sure the 'Suspend session' under the 'On low battery' tab is less than t 'sleep', else your computer will drain completely.

    Code:
    #! /bin/bash
    State=1
    while [ State != 0 ]; do
    
    if pacmd list-sink-inputs | grep -c 'state: RUNNING'; then
    let State=1
    #echo "Playing"
    xdotool mousemove_relative -- -1 -1
    sleep 11m
    xdotool mousemove_relative 1 1
    sleep 11m
    else
    let State=0
    #echo "Idle"
    fi
    
    done
    Discussion:
    First, what the script does: Basically, "pacmd list-sink-inputs | grep -c 'state: RUNNING'" checks if PulseAudio is playing, and the "xdotool mousemove_relative" lines tell xdotool to move the mouse cursor once every 11 minutes. What happens is the screen dims, shuts off, and then locks as usual, but while audio is playing xdotool moves the mouse cursor every 11 minutes so that screen turns on, which prevents the computer from sleeping. It's not perfect -- the screen turns on every 11 minutes and then dims and shuts off again -- but it's good enough. If someone knows how I could satisfy the above needs with a script without the screen turning on again, I'm all ears.

    Second, I'm a long-time linux user, familiar primarily with Kubuntu and Manjaro with KDE. I'm not exactly a newbie, though I still don't have particular interest in leaving the GUI behind (hence my choice of KDE), and for circumstantial reasons I'm only able to improve my knowledge of Gnu/Linux by increments. More or less, through experience I know how not to mess up my system and how to Google solutions to most of my problems, and when the time comes opening the terminal, making a script, etc. are no big deal. Simply put: My knowledge of Gnu/Linux and KDE is good enough for my general needs, but not comprehensive (something I hope to find the time to work on).

    That said, I was surprised I couldn't find a better solution than the above, which I think may be daunting for newbies (I hope I was detailed enough). Is there an out-of-the-box or otherwise click-and-fix solution that I am just missing? If not, I would love to hear thoughts as to why something like this isn't just standard. Most other solutions I found rely on manually starting something or other, then manually turning it off when the audio is finished playing, which feels to me to defeat the purpose of having a desktop environment such as KDE, or indeed a computer. I'm very willing to accept that I have just missed something, or that my setup is peculiar and therefore buggy, but if that's not the case, then I wonder why such a feature is not simply a standard? To me, having a computer sleep while playing audio is just plain odd.

    Thanks in advance for your input!

    #2
    Power under System settings menu. I have mine set to turn off the screen after 10 minutes. The suspend session box is not checked, and the do not turn off media on suspend is checked. I'm not looking at my system right now, but that's about how mine is set up. When the screen power saving mode kicks in, the media player (Clementine in my case) keeps on going, and my system does not go into suspend mode. I have set the PC to suspend previously, but I typically don't now. But you should be able to do all this in the System Settings, Power menu.
    The next brick house on the left
    Intel i7 11th Gen | 16GB | 1TB | KDE Plasma 5.27.11​| Kubuntu 24.04 | 6.8.0-31-generic



    Comment


      #3
      Thanks for the reply,

      Originally posted by jglen490 View Post
      Power under System settings menu. I have mine set to turn off the screen after 10 minutes. The suspend session box is not checked, and the do not turn off media on suspend is checked. I'm not looking at my system right now, but that's about how mine is set up. When the screen power saving mode kicks in, the media player (Clementine in my case) keeps on going, and my system does not go into suspend mode. I have set the PC to suspend previously, but I typically don't now. But you should be able to do all this in the System Settings, Power menu.
      I do not appear to have any 'do not turn off media on suspend' box to check. Just to be clear, we're at System Settings >> Power Management >> Energy Saving, right? I'm using Kubuntu 19.10. Also to be clear, it's the fact of media playing that prevents suspend, not e.g. the fact that Clementine is open? (I ask, because some of the solutions I found only did the latter). I agree, I should be able to do all this in System Settings, I'm quite puzzled.

      Comment


        #4
        I spoke too soon! The setting is called 'Pause media players when suspending', and it's under System Settings >> Power Management >> Advanced. Thanks for giving me an idea of what to look for

        Comment

        Working...
        X