tags:

views:

97

answers:

1

I am developing web interface for an mp3 player (mpg123 linux). The mpg123 is a command-line mp3 player and can be controlled using keyboard inputs. For example:

$ mpg123 -C filename.mp3

it will start playing the song and monitor keyboard inputs for control. Pressing 's' will pause the song 'q' for quit etc.

I am spawning an mpg123 process using a Perl script. From that script I want to send inputs to this process. I have the pid of the process, I just need to send keystrokes to this process for control purpose.

+4  A: 

You just have to spawn your mp3-player as a pipe from perl. Like so:

$| = 1; # Set unbuffered output.
open( my $mp3player, "| mpg123" ) or die "cannot start mp3 player: $!";
print $mp3player "s";
...
print $mp3player "q";
close $mp3player

Second try for multiple script invocations: In an interactive shell enter tty. That will give you a pseudo-terminal name. Now start your player in this shell. In another shell, write to that pseudo-terminal. E.g. echo "s" > /dev/pts/11. The player will receive this as input. If this works, use your perl script instead of echo to write to the pseudo-terminal.

Peter G.
Thanks Peter, I have some problems with this solution. Using this method, I am not able to make a non-blocking call to mpg123. I need to start the mp3 player and my script should exit. When the script is called again (with other parameters like "stop"). It should send a stop signal to the running process.
Punit Soni