[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] MP3 Player in MacPerl Using QT 4.0



Well, here's a version that has a menubar and everything. Just save it as 
runtime

-----BEGIN GEEK CODE BLOCK----
Version: 3.12
GCS d s:+ !a C++++ U*+++ P++++ L+ E-- W+++ N++ o+ K- w--- O? M++ V? PS PE-
Y-- PGP++ t+ 5 X R tv++ b+++ D I- D? G! e h! r
-----END GEEK CODE BLOCK-----

----------
#!perl

=pod

=head1 NAME

mp3player.plx

=head1 DESCRIPTION

Plays MP3 files.  Requires QuickTime 4.0 (currently in beta) and
MPEG::MP3Info module (on CPAN).  This may be developed into a larger
program by the author, or perhaps someone else will take it and run
with it instead (someone more suited to doing GUI work :).

So far there are no keyboard controls, and a playlist should
be added, and all that good stuff.  Maybe skins, different appearances.
Who knows?  If you are inclined to, go crazy, and provide us with
the changes.

Also, this might not perform as well as other MP3 players, but it seems
to perform as well as the QuickTime Player that comes with QuickTime
4.0.  So the hope is that when 4.0 is no longer beta, it will be
perform better.

=head1 AUTHOR AND COPYRIGHT

Chris Nandor E<lt>pudge@pobox.comE<gt>, http://pudge.net/

Copyright (c) 1999 Chris Nandor.

This program is free and open software. You may use, modify,
distribute, and sell this program (and any modified variants) in any
way you wish, provided you do not restrict others from doing the same.


=head1 VERSION

v0.10, Friday, April 23, 1999

=cut

use strict;
use Data::Dumper;
use File::Basename;
use Mac::Events;
use Mac::Files;
use Mac::Fonts;
use Mac::Movies;
use Mac::QuickDraw;
use Mac::Windows;
use Mac::Events qw(@Event $CurrentEvent);
use Mac::Menus;
use MPEG::MP3Info;
use Mac::StandardFile;
my($win, $file, $movie, $x1, $y1, $x2, $y2, $z,
    $bounds, $curr, @str, $tottime, $oldEdit, $oldFile, $oldEditor,
$newMenu, $newEdit, $newFile, $OK, $QUIT);


MacPerl::Quit(1);
#Change Menu Bar
$oldEdit   = GetMenuHandle(130);
$oldFile   = GetMenuHandle(129);
$oldEditor = GetMenuHandle(133);
$newMenu   = new_menu();
$newEdit   = $$newMenu[0]->{menu};
$newFile   = $$newMenu[1]->{menu};
change_menu_bar();
DisableItem($newEdit, 1);
DisableItem($newEdit, 2);
DisableItem($newEdit, 3);
DisableItem($newEdit, 4);

until ($QUIT) {
EnterMovies() or die $^E;
make_window();
start_movie($file);
WaitNextEvent while ($win->window && draw_window() && !$OK);
$win->dispose if defined $win;
@str = ();
ExitMovies();
$OK = 0;
}


sub make_window {
    $x1 = $y1 = 100;
    $x2 = 300;
    $y2 = 100 + 15;
    $z = 108;

    $bounds = Rect->new($x1, $y1, $x1 + $x2, $y1 + $y2);
    $win = MacWindow->new($bounds, "MP3 Player", 1, noGrowDocProc, 1);
    $win->sethook( redraw => \&draw_window );
    $win->sethook( drawgrowicon => sub {1} );

    SetPort($win->window);

    END {
        $win->dispose if defined $win;
    }
}

sub start_movie {
    if (get_movie($file)) {
        do_movie_info($file);
        $win->new_movie($movie, Rect->new(0, 108, $x2, $y2));
    }
}

sub get_movie {
    $file = shift || ask_for_movie();
    return unless $file;
    my $resfile = OpenMovieFile($file) or die $^E;
    $movie = NewMovieFromFile($resfile, 0, newMovieActive) or die $^E;
    CloseMovieFile($resfile);

    END {
        DisposeMovie($movie);
    }

    1;
}

sub draw_window {
    my($front, $back) = (GetForeColor(), GetBackColor());
    my $x = 15;

    TextFont(geneva());
    TextSize(9);
    TextFace(bold());
    MoveTo(10, 15);
    DrawString($str[0]);
    TextSize(9);
    TextFace(normal());

    for (1 .. $#str) {
        $x += 15;
        MoveTo(10, $x);
        DrawString($str[$_]);
    }

    my $ncurr = t_format(curr_time());
    if (!$curr || !$ncurr || $curr ne $ncurr) {
        RGBForeColor($back);
        PaintRect(Rect->new(10, $x + 5, $x2, $x + 20));
        RGBForeColor($front);
    }
    MoveTo(10, $x + 15);
    DrawString(sprintf "%s / %s", $ncurr, $tottime);
    $curr = $ncurr;
#    print MCGetControllerInfo($movie), "\n";
    1;
}

sub do_movie_info {
    my $lfile = shift;
    my($info, $tag) = (
        get_mp3info($lfile),
        get_mp3tag($lfile)
    );

    $tag->{TITLE} =
        $tag->{TITLE} ne '' ? $tag->{TITLE} :
            (fileparse($lfile, '\..{1,3}$'))[0];
    SetWTitle($win->window, $tag->{TITLE});

    push @str, $tag->{TITLE};
    push @str, $tag->{ARTIST} if $tag->{ARTIST} ne '';

    if      ($tag->{ALBUM} ne '' && $tag->{YEAR} eq '') {
        push @str, $tag->{ALBUM};
    } elsif ($tag->{ALBUM} eq '' && $tag->{YEAR} ne '') {
        push @str, $tag->{YEAR};
    } elsif ($tag->{ALBUM} ne '' && $tag->{YEAR} ne '') {
        push @str, "$tag->{ALBUM}, $tag->{YEAR}";
    }

    push @str, $tag->{COMMENT} if $tag->{COMMENT} ne '';
    push @str, ($info->{STEREO} ? "Stereo" : "Mono")
        . " @ $info->{FREQUENCY} Hz / "
        . "$info->{BITRATE}-kbps layer $info->{LAYER}";
    $tottime = t_format(@{$info}{qw(MM SS)});
    1;
}

sub t_format {
    my($mm, $ss) = @_;
    return sprintf "%2.2d:%2.2d", $mm, $ss;
}

sub curr_time {
    my($scale, $time) = (
        GetMovieTimeScale($movie),
        GetMovieTime($movie)
    );
    my($mm, $ss);
    $time /= $scale;
    (int($time / 60), ($time % 60));
}

sub ask_for_movie {
    my $lfile = StandardGetFile(sub {
        local $_ = $_[0]->ioNamePtr;
        return !/\.mp[23]$/;
    }, 0);
    return unless $lfile->sfGood;
    $lfile->sfFile;
}
sub change_menu_bar {
    DeleteMenu(133);
    DeleteMenu(130);
    DeleteMenu(129);
    InsertMenu $newEdit, 133;
    InsertMenu $newFile, 2048;
    DrawMenuBar()
}

sub restore_menu_bar {
    DeleteMenu(2048);
    DeleteMenu(2049);
    InsertMenu($oldEdit,   133);
    InsertMenu($oldFile,   130);
    InsertMenu($oldEditor, 134);
    DrawMenuBar()
}
sub new_menu {
    my $newEdit = MacMenu->new (
        2048, 'Edit', (
            ['Cut',   \&edit_menu, 'X'],
            ['Copy',  \&edit_menu, 'C'],
            ['Paste', \&edit_menu, 'V'],
            ['Clear', \&edit_menu,  ''],
        )
    );
    my $newFile = MacMenu->new (
        2049, 'File', (
            ['OpenŠ', \&file_menu, 'O'],
            [],
            ['Close', \&file_menu, 'W'],
            [],
            ['Quit', \&file_menu, 'Q'],
        )
    );
    [$newEdit, $newFile]
}

sub edit_menu {
    my ($menu, $item) = @_;
    return 1
}

sub file_menu {
    my ($menu, $item) = @_;
    if ($menu == 2049) {
        if    ($item == 1)                { $OK = 1; $file = 0; } # Open
        elsif ($item == 3 or $item == 5)  { quit() } # Close or Quit
    }
}
sub quit {
    $OK = 1;
    $QUIT = 1;
}
END {
    restore_menu_bar();
    $win->dispose if defined $win;
    DisposeMenu($newEdit) if defined $newEdit;
    DisposeMenu($newFile) if defined $newFile;
}

__END__

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org