Archive of

Bash script: Reminder to swap mics with automatic pausing of all media players

The problem

I recently purchased a double pack of wireless microphones (specifically, these ones) to replace my ageing and faltering wired one. I am very happy with the audio quality, their ease of use and their range, but for my specific use case, the battery life (around 3 hours) leaves a little to be desired. I mainly use these while hanging out with a friend over the internet while sharing my screen, and we'll watch movies and TV shows together. That can last a couple of hours or even most of the day, and at some point the battery for the microphone will run out.

The first time this happened, it took me a little while to understand what was going on, as there was no beep or any indication from the microphone to signal its demise. Apparently the LED on the device will blink when it's low on battery, but that's impossible to see when it's clipped to my shirt right underneath my chin.

But, through the magic of buying two of them, the solution is easy — swap the depleted mic with a freshly charged one, then recharge the depleted mic while you discharge the fresh one. Still, the problem of not knowing when to do that persists.

The code

That's right, we're writing more Bash. Normally, you could just set a timer for your phone or your computer, but we're watching content that has sound whilst wearing headphones. What I wanted was a solution that could pause all playing media and tell me to swap my mic out, so that's exactly what I wrote:

#!/usr/bin/env bash
matches=$(playerctl -l)
read -r -d '' -a matchedPlayers <<< "$matches"
((numPlayers--)) # To use zero based indexing

for i in $(seq 0 "$numPlayers"); do
    status=$(playerctl -p "$currentPlayer" status)
    if [[ "$status" == "Playing" ]]; then
        playerctl -p "$currentPlayer" play-pause
        if [[ "$currentPlayer" == "kodi" ]]; then
            sleep 1

mplayer "/opt/sfx/mic-change.mp3"

How to use

Run the script when you turn on your microphone, and re-run it whenever you swap your mic. Examples:

  • Run a sleep then the script from the terminal:
    sleep 9000; /opt/scripts/
  • Put the sleep at the start of the script (after the shebang) and run it
  • Use a timer app that can launch scripts when the timer finishes, like KClock

(9000 seconds is 2h 30m)

Script explanation

This script uses playerctl to pause all currently playing media players via the MPRIS D-Bus specification. Most players for Linux support this natively. Kodi, my media player of choice, does not, but support can easily be added through an addon. The script goes through every currently registered media player, and checks if it is currently playing. If it is, it pauses it. If it is Kodi, it waits a second before doing anything else, since Kodi has a ˜1 sec delay when pausing before audio stops playing (and also an annoying corresponding ˜1 sec delay before audio starts playing again once you unpause). Finally, it plays the sound defined in the last line of the script using mplayer, which in my case is a TTS voice named Onyx telling me to "Change your mic, motherfucker."

If you'd like to get the same microphones, which are very good despite the relatively short battery life, you can get them on AliExpress here.

The product links in this article are affiliate links. If you buy something using them, I may earn a small commission at no extra cost to you.