Using MPlayer with Multimedia Keyboard (Media Buttons)

Written by Hasan Arous

I am one of those people who enjoy accomplishing computer tasks using the CLI rather than using GUI to do them. Be it file search, downloading a file, and even playing a video or some music.

Too long to read? Jump to solution!

MPlayer

So, I usually use mplayer for media playback, I navigate to the folder where I want to play music, and either go with mplayer -shuffle * or mplayer -gapless-audio *, so I rarely need any interaction to go forward or anything, and if I wanted to pause it, I would bring up the terminal and press Space.

Fetch me all the music, and play them randomly!

Today, I wanted to listen to a random mix of my music library. I went to the root folder, did a find -type f > all.pls, and then launched mplayer -shuffle -playlist all.pls.

Music started, and I continued doing my usual work. Every time a song that I don’t want to listen to start, I would do the usual thing, bring up the terminal, and press Enter to move on. But then it went tiresome…

Keyboard Pause, Forward & Previous

(Or more formally XF86AudioPlay, XF86AudioNext and XF86AudioPrev)

And then I thought, why wouldn’t I make use of the keyboard buttons on my keyboard? I did some research and found half answers related to what I initially thought of, making a file pipe (mkfifo) that mplayer reads from, and then customize the keys to write to that pipe.

Solution

Important: Replace every occurrence of /home/aularon/ with your own $HOME path.

1. Create the pipe file

mkfifo ~/.mplayer/pipe

2. Edit MPlayer config file

Edit (Or create) the file ~/.mplayer/config, and add the following line:

input=file=/home/aularon/.mplayer/pipe

3. Create a helper script

Create a file ~/.mplayer/cmd, with the following contents:

#!/bin/bash
echo "${1/@/ }" > ~/.mplayer/pipe

Then make it executable:

chmod +x ~/.mplayer/cmd

4. Finally, edit keyboard shortcuts to run the script

On my Ubuntu 12.04, It’s in System Settings > Keyboard > Shortcuts > Custom Shortcuts: Custom Keyboard Shortcuts Window

Here you can see my shortcuts already added, click on the + button to add ones your self:

MPlayer Next Shortcut

The name does not matter, but for the Command part you will have to enter these three commands (For Pause, Next & Previous, respectively):

  • /home/aularon/.mplayer/cmd "pause"
  • /home/aularon/.mplayer/cmd "pt_step@1"
  • /home/aularon/.mplayer/cmd "pt_step@-1"

After finishing this, the shortcuts would show, in the list, with Disabled status on the right column, double click the Disabled label to supply the shortcut you want for this action.

That’s it!

If you are having any problem or have any question, please let me know.