Skip to content

A script which automatically pauses your music when you unplug your headphones

Notifications You must be signed in to change notification settings

Drarig29/auto-pause-unplug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

auto-pause-unplug

A script which automatically pauses your music when you unplug your headphones or disconnect your Bluetooth device.

You can tweak it to meet your needs.

Dependencies:

Usage

Download this script or clone the repository.

Then, symlink it somewhere like /usr/bin/auto-pause-unplug:

ln -s ~/path/to/auto-pause-unplug.sh /usr/bin/auto-pause-unplug

Warning: this file contains an infinite loop, so you need to run the command in the background with &, otherwise your desktop environment is likely to freeze when you log in.

I personally used ~/.xprofile, here is its content:

#!/bin/bash
exec /usr/bin/auto-pause-unplug &

How it works?

  • It uses pactl subscribe, so no polling is involved
  • Any existing instance of the script is killed
  • All errors are ignored so that the program doesn't stop unexpectedly
  • When a change event is detected on a Pulse Audio card, it checks if the active port is something else than the headphones
  • If it's the case, we assume the headphones were just unplugged (I didn't have any false positive yet)
  • We then pause the playing media with playerctl

You can view the logs using journalctl:

journalctl -b | grep -E "\[$(pidof -x /usr/bin/auto-pause-unplug)\]"

About

A script which automatically pauses your music when you unplug your headphones

Topics

Resources

Stars

Watchers

Forks

Languages