2

I'm looking for a way to start playing an audio file (mp3, m4a) as a background task from a shell, and then be able to do things like pause/fast forward at a later time.

Is this possible either using built-in command line options:

$ play audio.mp3 &
$ play --pause

Or by way of the player's process ID?

$ play audio.mp3 & echo $! > pid
$ control_process $(cat pid) --signal pause

(Examples are illustrational only.)

1

There is no generic solution but there are at least 2 players that could do that.

The first is mplayer together with named pipes.

First, create /tmp/mplayer.fifo named pipe:

$ mkfifo /tmp/mplayer.fifo

Start mplayer and point it to /tmp/mplayer.fifo:

$ mplayer -novideo -idle -slave -input file=/tmp/mplayer.fifo file.mp3 </dev/null >/dev/null 2>&1 &

Now you can pause and resume playback by writing to /tmp/mplayer.fifo:

$ echo pause > /tmp/mplayer.fifo

Subsequent pause will resume playback:

$ echo pause > /tmp/mplayer.fifo

You can also do other things, for example fast forward:

$ echo seek +60" > /tmp/mplayer.fifo

or rewind playback:

$ echo "seek -60" > /tmp/mplayer.fifo

The second one is mocp. Start playback:

$ mocp -S; mocp -p file.mp3

Pause/resume playback:

$ mocp --toggle-pause
2
  • Please do not downvote without leaving a comment. – Arkadiusz Drabczyk Jun 8 '18 at 20:03
  • 1
    Fantastic! This is a simple and intuitive solution. The named pipe works straight away and requires no extras (except the player, which well known and quick to install.) – forthrin Jun 9 '18 at 8:07
2

If using VLC player is an option, you can control a running VLC process either through remote apps (GUI) or CLI using dbus calls.

For example - assuming qdbus is installed - toggle play/pause on currently played music:

$ qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

or go to next piece in the playlist:

$ qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next

See all available commands:

$ qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2

See also this thread https://stackoverflow.com/questions/14256193/linux-control-a-running-vlc-process-through-command-line

2
  • Not bad. A bit verbose, but will work. A good alternative to the named pipe, which doesn't require extra packages. – forthrin Jun 9 '18 at 8:10
  • thanks for feedback. Verbosity can be easily controlled using aliases, and qdbus is part of qt package which is usually present in default installations... – tonioc Jun 9 '18 at 10:05
1

You might want to look into MPD, as it can do pretty much everything you seem to want to do (remote control, add new tracks, etc). Instead of running as a background process in your shell, it runs as a system service, which means that it can easily be used across multiple sessions and logins. It even has the ability to allow remote control from another system (it's widely used for network-controlled jukebox-type appliances).

There are also a number of nice front ends for it, including ones you can use from the console.

1
  • I had a look at this earlier when I checked out cmus. It seems like a very flexible solution with many possibilities, but probably takes some time to get into. Good to know for later if I need something more advanced. – forthrin Jun 9 '18 at 8:09
0

The implementation of this control would typically be within the program itself, but a very common way to carry out this kind of interface is a Named pipe. As long as it is on the same host, and you're not opening sockets for network control. You could create your own program to use the named pipe and execute the commands needed, killed the process, etc. If you want to see a named pipe in action I would suggest installing the CLI player for Pandora; pianobar. It uses it to accept commands like in your example.

1
  • I haven't used named pipes before, but that's fascinating, how you can use it to continually add files to an archive, for example! Where I live, no-one (including myself) has Pandora. I'll leave the question open for further suggestions that may work with local audio files. – forthrin Jun 8 '18 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.