Mplayer - Change track, play/pause with command line

673 views Asked by At

Mplayer provides shortcuts to change the track and play/pause the music but they only work if the mplayer window has the focus.

Is it possible, for example, to change the track currently played in mplayer with a command line? In that case, I could assign this command to a shortcut and use it even if mplayer does not have the focus.

All I found on google was how to use mplayer to play files or which shortcuts to change track when the window has the focus.

1

There are 1 answers

1
Mushfiq Mahmud On

I am actually in the middle of trying to do this exact thing! I am going to share what I have discovered so far in case it helps anyone.

I have not been able to find a way to play/pause tracks. However, I have been able to change tracks, at least in terms of going to the next track.

I have found 2 ways so far: Using /proc/$pid/fd/0 and Named Pipes (FIFOs). Explaining FIFOs is a little harder (I just recently learned of them) so I will do that last.

Modifying STDIN via /proc/$pid/fd/0

We can communicate with a process in a different terminal session by redirecting input to the process' file definitions directory at /proc/$pid/fd/ where $pid is the process' PID.

Firstly, find the process ID using pidof. Make sure mplayer is actually running!

$ pidof mplayer

This will output the PID of mplayer. Using this $PID, we can input what we want to STDIN for mplayer by redirecting it to /proc/$PID/fd/0. (Note, 0 refers to STDIN, 1 to STDOUT, etc.). An example:

$ echo "p" > /proc/$PID/fd/0

This has the effect of changing the tracks for mplayer.

However, even though I used p in my example, I have found that you can echo anything (including nothing) and it will still accomplish the same goal of changing the track no matter what you input.

Named Pipes

Also called FIFOs, named pipes, just like regular pipes (|), redirect output to other processes. The main difference is that these are part of the filesystem and can be used to communicate between different terminal sessions.

To start off, create a new named pipe anywhere in your filesystem. I chose /tmp/.

$ mkfifo /tmp/mp3control.pipe

Then start mplayer redirecting its STDIN to the named pipe.

$ mplayer -playlist brutal_tunez.txt < /tmp/mp3control.pipe

It will look like the process hangs and that's because it is expecting the other side to input something.

Now open up a new terminal and input stuff into the pipe.

$ echo "p" > /tmp/mp3control.pipe

You will hear music start to play from mplayer. Again, just like before, any further inputs to the pipe will just change tracks.

I believe this has something to do with the way mplayer handles STDIN and is not the same thing as entering p to play or pause music.

I will continue looking into this but I would love to hear from someone if I made a mistake somewhere. All in all, this might end up being something where the amount of time spent is not worth it, to be honest. (But we still must!)