0

I'm making a basic music player in C++ on an embedded Linux platform (Intel Edison).

It works by sending commands to the shell to play the music using mpg123.

Playing the music is working fine, but now I need to add media controls. I'm using the mpg123 command to play my MP3 files and if you pass it the -C flag then it listens for commands such as p for pause, q for quit, etc

I'm using the code from this other SO answer for executing the commands How to execute a command and get output of command within C++ using POSIX?

Now the thing is, when I run the command to play the selected song from c++ (eg. mpg123 -C /media/sdcard/Music/<songname>) it hangs the c++ program until the command returns, which doesn't return until the song is done playing. I can eliminate that issue by using pthead to run the command in a parallel thread, but that still doesn't allow me to enter commands as I need to.

I thought of maybe somehow running the command in another tty, and then somehow passing the commands to mpg123 by sending them to the other tty, but I was unable to find a way to do this. (and I'm not sure it's possable)

I've been trying to do this for hours, so now I'm ready to ask here for some help :)

Do any of you have any ideas on how to run the command without freezing the thread (in a pthread or whatever), and still be able to send commands to mpg123?

0andriy
  • 4,183
  • 1
  • 24
  • 37
Keith M
  • 1,199
  • 2
  • 18
  • 38
  • So, you basically want `P` to mean "pause" for your mp3 player, and `ls -l Private` to list your files in the `Private` directory, at the same time, without the `P` for `Private` meaning "pause"? Sounds rather impossible to me. But yes, you can use a pipe or a pseudo-terminal to send things from one process to another. Which makes most sense really depends on what you are trying to achieve - aside from the impossible, that is. – Mats Petersson Mar 03 '16 at 22:08
  • @MatsPetersson Not quite sure what your saying there. After I run `mpg123` with the `-C` flag it listens for input characters that are used for commands, such as `p` for pause. I need to find a way to keep the command execution from freezing the thread, but still be able to send it the input characters for my media controls – Keith M Mar 03 '16 at 23:38

1 Answers1

2

It sounds like you'll want to use mpg123's FIFO mode. You set a pathname to be used, point mpg123 at it and then echo your commands into it.

This answer has a great example

FIFO_MPG='/srv/http/newsctl'
mpg123 -R --fifo "$FIFO_MPG"
echo 'load filename.mp3' >> "$FIFO_MPG"
Community
  • 1
  • 1