Strange but true,
I was working on exactly the same problem while reading this node ;).
Audio::Mpeg does however needs an external library, if I understand the docs correctly? So I decided to try Corion's suggestion. And voila, flawless victory! I'd kiss him if only he were a pretty girl. ;) below my code, you might find it handy, allthough it could use some cleaning up. Messy teabag eh...
#!/usr/bin/perl -w
#perltk wav/mp3 browser example
use strict;
use Tk;
use Win32::Sound;
use Tk::FileSelect;
use Cwd;
my $dir = "C:/Mijn\ documenten";
my $loop = 0;
my @selected;
my $Form1 = MainWindow->new;
$Form1->title("Form1");
$Form1->geometry("400x200+8+8");
my $Label1 = $Form1->Label(
-borderwidth => 1,
-text => "initial dir:",
-background => "#C0C0C0",
-cursor => "",
-font => "Tahoma 8 normal"
);
$Label1->place(
-width => 50,
-height => 16,
-x => 16,
-y => 16
);
my $Edit1 = $Form1->Entry(
-textvariable => \$dir,
-cursor => "",
-font => "Tahoma 8 normal"
);
$Edit1->place(
-width => 200,
-height => 16,
-x => 66,
-y => 16
);
my $output = $Form1->Scrolled( "Text", -scrollbars => 'e' )->pack();
$output->place(
-width => 390,
-height => 100,
-x => 15,
-y => 90
);
my $Button1 = $Form1->Button(
-text => "play",
-cursor => "",
-command => sub { &play(); },
-font => "Tahoma 8 normal"
);
$Button1->place(
-width => 75,
-height => 25,
-x => 5,
-y => 40
);
my $Button2 = $Form1->Button(
-text => "stop",
-cursor => "",
-command => sub { &stop(); },
-font => "Tahoma 8 normal"
);
$Button2->place(
-width => 75,
-height => 25,
-x => 105,
-y => 40
);
my $Button3 = $Form1->Button(
-text => "select mp3 file",
-cursor => "",
-command => sub { &openfd(); },
-font => "Tahoma 8 normal"
);
$Button3->place(
-width => 75,
-height => 25,
-x => 205,
-y => 40
);
my $Button4 = $Form1->Button(
-text => "select wav file",
-cursor => "",
-command => sub { &openfd2(); },
-font => "Tahoma 8 normal"
);
$Button4->place(
-width => 75,
-height => 25,
-x => 305,
-y => 40
);
my $Check1 = $Form1->Checkbutton(
-variable => \$loop,
-text => "loop wav?"
);
$Check1->place(
-width => 100,
-height => 17,
-x => 20,
-y => 70
);
my $fs = $Form1->FileSelect(
title => "Load MP3 - File",
-filter => "*.mp3",
-directory => "$dir"
#doesn't work yet!
#-selectmode => 'multiple'
);
my $fs2 = $Form1->FileSelect(
title => "Load WAV - File",
-filter => "*.wav|*.mp3",
-directory => "$dir"
#doesn't work yet!
#-selectmode => 'multiple'
);
tie( *STDERR, 'Tk::Text', $output );
tie( *STDOUT, 'Tk::Text', $output );
$SIG{'__WARN__'} = sub { print STDERR @_ };
MainLoop;
############################ SUBS ############################
sub play {
foreach my $song (@selected) {
if ( $song =~ m/mp3/gi ) {
# plays one of Corions favourite songs! ;)
print "playing mp3:\n$song\n";
system(qq{start "$song" "$song"});
}
else {
if ( $loop eq "1" ) {
print "playing wav(looping):\n $song\n";
Win32::Sound::Play( "$song", SND_ASYNC | SND_LOOP );
}
else {
foreach $song (@selected) {
print "playing wav:\n$song\n";
Win32::Sound::Play( "$song", SND_ASYNC );
}
}
}
}
}
sub stop {
Win32::Sound::Stop();
}
#file dialog
sub openfd {
@selected = $fs->Show();
&printy();
}
#file dialog2
sub openfd2 {
@selected = $fs2->Show();
&printy();
}
#prints the selection
sub printy {
foreach my $song (@selected) {
print "selected:\n--------------------------------------\n$son
+g\n"
if defined $song;
}
}
Teabag
Sure there's more than one way, but one just needs one anyway - Teabag
|