Pause My Music

Created 2009-05-08 / Edited 2009-07-11

Tags: perl, linux, music

Oh how I love linux... perl... scriptability. I have a fun little command line music player named Polly. It is pretty minimalist, really just starting whatever player I like with whatever preferences I like on a whole directory tree of files. One thing that is annoying is that I have to switch over to the terminal and it ^Z to pause and then do 'fg' to play.

So I finally decided to write a simple pause toggle script. I approached this in the most brute force way I could... I just take the list of all my media players and send a massive STOP (effectively ^Z) command to all to pause, and then a massive CONT (effectively 'fg') to all to continue. To determine if I want to pause or continue, I just look to see if I have any paused player processes.

my @commands = qw( ogg123 mpg123 mplayer vlc sidplay2 );
my $cmd = join '\|', @commands;

if(`ps a -o state,cmd | grep '$cmd' | grep ^T`) {
  map { `killall -SIGCONT $_ 2>/dev/null` } @commands;
} else {
  map { `killall -SIGSTOP $_ 2>/dev/null` } @commands;
}