Commit 0882c7d2 by PotatoGim

Wx::MediaCtrl 예제 구현.

parent 29f0f78e
#!/usr/bin/env perl
#=============================================================================
# FILE: mediactrl.pl
# USAGE: ./mediactrl.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 25일 09시 43분 21초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
package MediaSample::MediaPanel;
use Wx qw/:everything/;
use Wx::Event qw/:everything/;
use Wx::Media;
use base qw/Wx::Panel/;
sub new
{
my ($class, $parent) = @_;
my $self = $class->SUPER::new(
$parent,
wxID_ANY,
wxDefaultPosition,
wxDefaultSize,
wxTAB_TRAVERSAL | wxBORDER_NONE
);
my $mswbackend = wxMEDIABACKEND_WMP10;
my $mediabackend = (Wx::wxMSW())
? $mswbackend
: (Wx::wxMAC())
? wxMEDIABACKEND_QUICKTIME
: wxMEDIABACKEND_GSTREAMER;
#-------------------------------------------------------------------------
# Controls
#-------------------------------------------------------------------------
$self->{mediacontrol}
= Wx::MediaCtrl->new($self, wxID_ANY, "", wxDefaultPosition
, wxDefaultSize, wxTE_READONLY);
$self->{buttonload} = Wx::Button->new($self, wxID_ANY, "Load");
$self->{buttonuri} = Wx::Button->new($self, wxID_ANY, "Load URI");
$self->{buttonplay} = Wx::Button->new($self, wxID_ANY, "Play");
$self->{buttonpause} = Wx::Button->new($self, wxID_ANY, "Pause");
$self->{buttonstop} = Wx::Button->new($self, wxID_ANY, "Stop");
#-------------------------------------------------------------------------
# Events
#-------------------------------------------------------------------------
EVT_MEDIA_LOADED($self, $self->{mediacontrol}, sub { shift->OnEventMediaLoaded(@_); });
EVT_MEDIA_STOP($self, $self->{mediacontrol}, sub { shift->OnEventMediaStop(@_); });
EVT_MEDIA_FINISHED($self, $self->{mediacontrol}, sub { shift->OnEventMediaFinished(@_); });
EVT_BUTTON($self, $self->{buttonload}, sub { shift->OnEventButtonLoad(@_); });
EVT_BUTTON($self, $self->{buttonuri}, sub { shift->OnEventButtonLoadURI(@_); });
EVT_BUTTON($self, $self->{buttonplay}, sub { shift->OnEventButtonPlay(@_); });
EVT_BUTTON($self, $self->{buttonpause}, sub { shift->OnEventButtonPause(@_); });
EVT_BUTTON($self, $self->{buttonstop}, sub { shift->OnEventButtonStop(@_); });
#-------------------------------------------------------------------------
# Layout
#-------------------------------------------------------------------------
my $mainsizer = Wx::BoxSizer->new(wxVERTICAL);
$mainsizer->Add($self->{mediacontrol}, 1, wxEXPAND|wxALL, 0);
$mainsizer->Add(Wx::StaticLine->new($self, -1), 0, wxEXPAND|wxALL, 5);
my $buttonsizer = Wx::BoxSizer->new(wxHORIZONTAL);
$buttonsizer->Add($self->{buttonload}, 0, wxEXPAND|wxALL, 3);
$buttonsizer->Add($self->{buttonuri}, 0, wxEXPAND|wxALL, 3);
$buttonsizer->Add($self->{buttonplay}, 0, wxEXPAND|wxALL, 3);
$buttonsizer->Add($self->{buttonpause}, 0, wxEXPAND|wxALL, 3);
$buttonsizer->Add($self->{buttonstop}, 0, wxEXPAND|wxALL, 3);
$mainsizer->Add($buttonsizer, 0, wxALIGN_RIGHT|wxALL, 3);
$self->SetSizer($mainsizer);
#-------------------------------------------------------------------------
# State Values
#-------------------------------------------------------------------------
$self->{medialoaded} = 0;
$self->reset_state();
return $self;
}
sub reset_state
{
my $self = shift;
my $state_setter = sub {
$self->{buttonload}->Enable(shift);
$self->{buttonuri}->Enable(shift);
$self->{buttonplay}->Enable(shift);
$self->{buttonpause}->Enable(shift);
$self->{buttonstop}->Enable(shift);
return;
};
if (!$self->{medialoaded})
{
$state_setter->(1, 1, 0, 0, 0);
return;
}
my $state = $self->{mediacontrol}->GetState;
if ($state == wxMEDIASTATE_PLAYING)
{
$state_setter->(0, 0, 0, 1, 1);
}
elsif ($state == wxMEDIASTATE_STOPPED)
{
$state_setter->(1, 1, 1, 0, 0);
}
else
{
$state_setter->(0, 0, 1, 0, 1);
}
return;
}
sub OnEventMediaLoaded
{
my ($self, $event) = @_;
$self->{medialoaded} = 1;
print "EVT MEDIA LOADED \n";
$self->reset_state();
return;
}
sub OnEventMediaStop
{
my ($self, $event) = @_;
if ($self->{mediastopping})
{
print "EVT MEDIA STOP\n";
$self->{mediastopping} = 0;
}
else
{
$event->Veto();
print "EVT MEDIA STOP VETOED\n";
}
$self->reset_state();
}
sub OnEventMediaFinished
{
my ($self, $event) = @_;
print "EVT MEDIA FINISHED\n";
$self->reset_state();
}
sub OnEventMediaStateChanged
{
my ($self, $event) = @-;
print "EVT MEDIA STATECHANGED\n";
$self->reset_state();
}
sub OnEventMediaPlay
{
my ($self, $event) = @_;
print "EVT MEDIA PLAY\n";
$self->reset_state();
}
sub OnEventMediaPause
{
my ($self, $event) = @_;
$event->Skip(1);
print "EVT MEDIA PAUSE\n";
$self->reset_state();
}
sub OnEventButtonLoad
{
my ($self, $event) = @_;
$self->{mediacontrol}->Stop;
my $file = Wx::FileSelector("Select a media file");
if (length($file) && !$self->{mediacontrol}->LoadFile($file))
{
Wx::LogError("Application failed to load %s", $file);
}
$self->reset_state();
}
sub OnEventButtonLoadURI
{
my ($self, $event) = @_;
my $uri;
my $dialog = Wx::TextEntryDialog->new(
$self,
"Enter an address to download",
"Select Media URI",
"");
if ($dialog->ShowModal != wxID_CANCEL)
{
$uri = $dialog->GetValue();
}
$dialog->Destroy();
print "LOADING $uri\n";
if (length($uri) && !$self->{mediacontrol}->LoadURI($uri))
{
Wx::LogError("Application failed to load %s", $uri);
}
}
sub OnEventButtonPlay
{
my ($self, $event) = @_;
$self->{mediacontrol}->Play();
$self->reset_state();
}
sub OnEventButtonPause
{
my ($self, $event) = @_;
$self->{mediacontrol}->Pause();
$self->reset_state();
}
sub OnEventButtonStop
{
my ($self, $event) = @_;
$self->{mediastopping} = 1;
$self->{mediacontrol}->Stop;
$self->reset_state();
}
1;
package MediaSample::Frame;
use Wx qw/:everything/;
use base qw/Wx::Frame/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new(undef, wxID_ANY, "MediaSample::Frame", wxDefaultPosition, [600, 600]);
$self->{mainpanel} = MediaSample::MediaPanel->new($self);
my $mainsizer = Wx::BoxSizer->new(wxVERTICAL);
$mainsizer->Add($self->{mainpanel}, 1, wxEXPAND|wxALL, 0);
$self->SetSizer($mainsizer);
return $self;
}
1;
package MediaSample::App;
use Wx qw/:everything/;
use base qw/Wx::App/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new();
# note, OnInit will run before SUPER::new returns
$self->GetTopWindow->Show(1);
return $self;
}
sub OnInit
{
my $self = shift;
my $mainwin = MediaSample::Frame->new();
$self->SetTopWindow($mainwin);
return 1;
}
1;
package main;
my $app = MediaSample::App->new();
$app->MainLoop();
1;
__END__
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment