Pause music when the screen is locked, and resume unlocking for Spotify, Rhythmbox, etc.

When you lock your computer screen (without pausing the system), most desktop audio players continue to play in the background, sometimes without sounding¹. As a result, you may inadvertently skip songs in podcast lists or playlists, etc.
Enter Lock pauseBash script, it will pause the music player when you lock the screen and resume playback after the screen is unlocked. The lock pause feature is available for Unity, GNOME, Cinnamon, KDE Plasma, and MATE desktop environments, and it supports Spotify and rhythm boxes by default. With the help of player (Command line controller for controlling media players supporting MPRIS D-Bus interface), this script can extend its supported music player to many other players, including Audacious, VLC, Cmus, etc.To install a lock pause, Download script, Copy it somewhere to you $PATHLike /usr/local/binAnd make it executable. E.g. install to /usr/local/bin And use the following command to make it automatically executable (assuming you have a “pause on lock” script available in your current folder):

sudo install pause-on-lock /usr/local/bin

Warning: Always check the code of scripts running on your system. Pause lock Script code It’s actually small, so it’s easy to check.
To try it out, all you have to do is run the script:

pause-on-lock

Now when you lock the screen on MATE, GNOME, KDE Plasma, Cinnamon or Unity desktop environment, you should automatically pause Spotify or Rhythmbox and resume playback after unlocking the screen. Player (such as Audacious or VLC) install playerctl:

sudo apt install playerctl
  • Fedora:
sudo dnf install playerctl
  • Arch Linux, Manjaro:
sudo pacman -S playerctl
  • openSUSE:
sudo zypper install playerctl
  • Solus OS:
sudo eopkg install playerctl

You’ll want to add a pause when you add a lock in your startup app, so you don’t have to manually run the script every time you pause the music player when you lock the screen and unpause when the screen is unlocked. Launch the Launch Application application or equivalent and add Lock Suspend to the launch application from here.
If your desktop environment / Linux distribution does not have a graphical tool that allows adding scripts to launch, you can also do this manually by creating a file (for GNOME-based desktops). pause-on-lock.desktop In ~/.config/autostart/ Has the following:

[Desktop Entry]
Type=Application
Exec=pause-on-lock
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=pause-on-lock

¹On Gnome 3.32 with GDM3 (Ubuntu 19.04), the default behavior is to mute the music player when the screen is locked while continuing to play in the background. If the user subsequently presses a key or moves the mouse to make the lock screen open, the sound will not be muted even if the screen is not unlocked. On Ubuntu MATE 18.04 (using the MATE screen saver), on the other hand, you will continue to listen to music after you lock the screen.

Source

Sidebar