Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Playing MP3 file?

by Massyn (Hermit)
on Oct 01, 2003 at 09:58 UTC ( [id://295558]=perlquestion: print w/replies, xml ) Need Help??

Massyn has asked for the wisdom of the Perl Monks concerning the following question:

#!/fellow/monks.pl

use Win32::Sound; Win32::Sound::Play("tada.wav"); Win32::Sound::Stop;

... works fine when I'm playing a WAV, but what about MP3? The same code unfortunatly does not cut it. Any ideas how I can use the internal Windows methods to play MP3s?

Thanks!

#!/massyn.pl

Don't -- me without telling me why. If you asked a stupid question, I wouldn't down vote you.

Replies are listed 'Best First'.
Re: Playing MP3 file?
by Corion (Patriarch) on Oct 01, 2003 at 10:30 UTC

    Windows has no "internal method" to play mp3s. At best, you can have a codec that decodes .wav files encoded with the Layer-III codec (for example by the Fraunhofer Institut).

    Your best bet short of manually decoding the mp3 stream or linking a mp3 player DLL is to launch the mp3 file through the shell facilities:

    # one of my favourite songs! my $mp3 = "Unknown Artist - 02 - unknown.mp3"; system(qq{start "$mp3" "$mp3"});
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Playing MP3 file?
by benn (Vicar) on Oct 01, 2003 at 12:16 UTC
    Corion's advice is sound (pun unintentional :)), but if you wanted to try the 'full' approach, you could have a look at a combination of LAME and Audio::MPEG. There are LAME binaries for Windows available, and Audio::MPEG's author seems fairly confident that it *should* work, although he hasn't tested on anything 'cept Linux.

    Included in the Audio::MPEG docs is an MP3->WAV routine which may come in handy.

    Cheers, Ben.

      2 methods I use to play an MP3 in my program on Windows XP with Perl.

      a> I don't have the code handy, but you can launch Windows Media Player with a playlist as a Win32 process. I never learned how to make a WMP playlist from Perl, :(.

      b> The way I do it now in my Perl programs is to embed the MP3 in a SWF file in a local html file and play it through the Internet Explorer. (Then you can make a pretty album cover to display while the music plays.) Just launch the Internet Explorer with html file as argument with win32 process.
Re: Playing MP3 file?
by teabag (Pilgrim) on Oct 01, 2003 at 15:05 UTC
    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
Re: Playing MP3 file?
by elwarren (Priest) on Oct 01, 2003 at 15:49 UTC
    I was fooling around with some audio stuff this weekend and found that Audio::Play::MPG123 is available for Win32. The readme says it doesn't support http yet, but it's compiled and ppm installed it from activestate.

    You might be able to convince Win32::MCI::Basic to play your file.

    HTH!
Re: Playing MP3 file?
by newrisedesigns (Curate) on Oct 01, 2003 at 16:16 UTC

    Arbogast mentioned playing the file through Flash. Seven Deadly (Lore Sjoberg of the Brunching Shuttlecocks) has produced Echindna, which does this using Perl and some predesigned flash.

    Hope this helps.

    John J Reiser
    newrisedesigns.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://295558]
Approved by Corion
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found