Controlling Audio with i3

As I mentioned in the last post, I've switched to the i3 window manager. After fixing the Caps Lock key, the next order of business is getting the audio keys on my multimedia keyboard to control audio output. (It's nice to be able to quickly mute or pause when the phone rings, for example.)

This turns out to be pretty easy. I used xev to figure out the keysyms for the audio keys, and then added bindings to ~/.i3/config:

bindsym XF86AudioRaiseVolume exec pactl set-sink-volume 0 +5%
bindsym XF86AudioLowerVolume exec pactl set-sink-volume 0 -- -5%
bindsym XF86AudioMute exec ~/bin/toggle-mute
bindsym XF86AudioPlay exec clementine --play-pause
bindsym XF86AudioStop exec clementine --stop
bindsym XF86AudioPrev exec clementine --previous
bindsym XF86AudioNext exec clementine --next

As you can see, I prefer clementine as an audio player. If you have something different that you like, look up the command line method for controlling the player.

The pactl command controls pulse audio. The toggle script is something I threw together because pactl doesn't support toggling the mute, you have to tell it mute or unmute:

1
2
3
#!/bin/bash
toggle=$((pactl list sinks | grep -q Mute:.no && echo 1) || echo 0)
pactl set-sink-mute 0 $toggle

I only have one sink, so if you have more than one, you'll need to adjust the script.

Posted on 2012-05-12 by brian in linux .

Comments

Thank you very much!

Please note, that the given script only works on a system with english first language because the output of pactl list sinks is for some reason localized^^

For example on a german system it has to be:

1
2
3
#!/bin/bash
toggle=$((pactl list sinks | grep -q Stumm:.no >> echo 1) || echo 0)
pactl set-sink-mute 0 $toggle
Niklas974
2012-06-29 20:53:46

I think exec is executed in bash, therefore there is no need for the wrapper script.

lzap
2012-08-31 15:30:33

This post clarifies how things are executed: http://faq.i3wm.org/question/403/executing-applications-exec/

A wrapper script makes it less easy to shoot yourself in the foot by using characters which have a meaning in i3 (such as ; or ,).

A better way to run a command in an unlocalized fashion is "LC_ALL= LC_MESSAGES=C foobar", or, in a shell script: unset LC_ALL export LC_MESSAGES=C foobar

Michael
2012-09-06 13:32:13

Michael -

Thanks for the tips. (And many thanks for giving us i3!)

-Brian

Brian St. Pierre
2012-09-06 13:51:37

A little less verbose/resouce hungry version ;)

pactl list sinks | grep -q Mute:.yes pactl set-sink-mute 0 ${PIPESTATUS[1]}

Mateus Caruccio
2012-09-13 20:24:35
Comments on this post are closed. If you have something to share, please send me email.