Playerctl is a command line controller and lib for spotify, vlc, audacious, bmp, xmms2, mplayer, and other media players. Get it here. This article will demonstrate some use cases of the utility and explain some of its design goals. If you find an interesting way to use Playerctl not mentioned here, let me know about it by posting a comment on this article or sending me an email.
The command line utility
Playerctl includes a command line utility that can be used to control your media player with these commands:
1 2 3 4 5 6 7 8 9 10 11 12
It works by sending the command to the media player process through the DBus message system and should respond in the same way as if you had clicked the corresponding little button on the graphical user interface of the player. This makes it easy to create your own interface to the player that is most convenient for you.
The most obvious implementation for this is to bind
playerctl commands to keyboard shortcuts. All modern Linux desktop environments should provide an easy way to bind a command to keyboard keys. The Super key (sometimes called the windows key) is conventionally reserved as a modifier for the window manager. Then the next time the phone rings and you need to pause your music, instead of fumbling through your windows to find your player and clicking the pause button, you can simply press the pause key without disturbing your work layout.
When you first learned to drive, you may have gotten the advice always leave your keys in the same place so you know where they are when you need them. Using global key bindings for controlling your media player is the same advice. For many years, I put my keys in the last pair of pants I left the house in, and had to spend five minutes searching for them anytime I wanted to drive anywhere. Your attention is most often the limiting factor for what you can accomplish during your day, and small steps like this that reduce the impact of common distractions can make a big difference.
Using the library
In the tradition of Unix engineering, the commandline utility
playerctl is made from a library of the same name which is also available for you to use. This allows advanced users to have even more control over the player by providing access to player properties and events. This great part of the library is that you can use it pretty much any programming language available to you thanks to its use of GObject-Introspection bindings. Your favorite prgramming language should most likely be on this list.
Nothing is more disagreeable to the hacker than duplication of effort. The first and most important mental habit that people develop when they learn how to write computer programs is to generalize, generalize, generalize. To make their code as modular and flexible as possible, breaking large problems down into small subroutines that can be used over and over again in different contexts. – Neal Stephenson
For example, to see a desktop notification when the next track plays, you can use this script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This can be especially useful with internet radio when you may not be familiar with the track. For desktop notifications (the application that handles
notify-send), I highly recommend dunst.
Another possible use with internet radio could be to skip tracks you have heard too many times, but you don’t necessarily want to downvote because you don’t want to mess up the recommendation algorithm (sort of like a feature Pandora had where you could tell it not to play a track for awhile).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
There are plenty of other use cases you could think on based on these examples, like lowering the volume when you get a new email message or keeping a status line generator up to date. Whatever you do, just don’t use it to mute commercials because you’re too cheap to get a Spotify subscription because that is really tacky!