[MPlayer-users] Slave Mode from Perl

Russell Altieri raltieri at earthlink.net
Tue Jul 1 06:11:17 CEST 2003


FYI,
	I have been wanting to use slave mode and use a Perl/Tk GUI for a
mobile car MP3 player I'm working on.  The problem is opening up a pipe
to mplayer,  sending commands to it at anytime, and getting back the
time in file with get_percent_pos.  Well anyway, here is a little
example script which will use open2 and Term::Readkey's Readline to do a
bidirectional pipe.  I called my script slave-mode.pl

Russ

begin slave-mode.pl -----------
#!/usr/bin/perl -w
# Perl Modules

use IPC::Open2;
use IO::Handle;
use Term::ReadKey;
use sigtrap;
my $song_playing=1;
my $file = shift(@ARGV);
if ($file) {
	print $file , "\n";
	$SIG{'PIPE'} = 'handler';
	open2(\*R, \*W, "mplayer -slave -quiet $file");
	clearbuffer();
	ReadMode "raw" , *R ;
	update_time();
	
} else { print "usage\n";
	 print "\.\\slave\-mode\.pl mp3file.mp3\n";
}

sub handler {
    $song_playing = 0;
    close W;
    close R;
}
sub update_time {
	my $time=get_time();
	while ($time && $song_playing) {
		select(undef,undef,undef,1.0); # good way to pause 
		$time=get_time(); 
	}
	print "done\n";
}
sub get_time {
	print W "get_percent_pos\n" ;
	if ($song_playing) {
		W->autoflush() ;
		my $line=ReadLine -1 , *R ;
		R->autoflush() ;
		print "$line";
		if ($line) {
			my @parse = split (/A:/ , $line);
			if ($parse[1]) {
				@parse = split (/ / , @parse[1] ) ;
				my $count=0;
				my $string = @parse[$count];
				until ($string) {
					$string = @parse[$count];
					$count+=1;
					if ($count >= 10) {$string="*";}
				}
				print "\n--" , @parse[$count-1] , "\n";
			}
		
		}
		clearbuffer();
		print "buffer Cleared\n";
	}
}
sub clearbuffer {
	my $line=ReadLine -1 , *R ;
	
	while ($line) {
		$line=ReadLine -1 , *R  ;
	}
	R->autoflush() ;
}



More information about the MPlayer-users mailing list