| Gtk2-Ex-MPlayerEmbed documentation | Contained in the Gtk2-Ex-MPlayerEmbed distribution. |
Gtk2::Ex::MPlayerEmbed - a widget to embed the MPlayer media player into GTK+ applications
use Gtk2::Ex::MPlayerEmbed;
my $window = Gtk2::Window->new;
my $embed = Gtk2::MPlayerEmbed->new;
$window->add($embed);
$window->show_all;
$embed->play("movie.mpg");
Gtk2->main;
Gtk2::Ex::MPlayerEmbed allows you to embed a video player into your applications. It uses the XEMBED system to allow the mplayer program to insert its window into your application.
Glib::Object
+----Gtk2::Object
+----Gtk2::Widget
+----Gtk2::Container
+----Gtk2::Socket
+----Gtk2::Ex::MPlayerEmbed
The following properties are accessible through the standard Glib get() and set() methods:
mplayer_pathThis is the path to the mplayer program. This is /usr/bin/mplayer by default.
argsThis is a string containing the command line arguments passed to mplayer (no default).
loadedThis is a boolean value that indicates whether the mplayer program is currently running.
stateThis is an enumeration (described by Gtk2::Ex::MPlayerEmbed::PlayingState)
that indicates the state of the player. state may be one of: stopped,
playing, paused. It is stopped at startup.
$embed->play([$content]);
This method has two behaviours: if the the loaded property is true (a video
stream has been loaded), and the state property is paused, then it will
resume playing the stream. If loaded is true but the stream is not
paused, then the method will carp() and return undef.
If load is false, and the $content argument is defined, then the player
will attempt to load and play the stream identified by $content, which may
be a path to a file, the URL of a network resource, or a "meta-URI" such as
dvd:// or dvb://.
$embed->pause;
This method will pause the current video stream. If the stream is not playing, this method will carp() and return undef.
$embed->resume;
This is just a convenience wrapper around pause(). The pause() method is
really a toggle, and two subsequent calls to pause() will pause and then
resume the stream, so this method exists to disambiguate.
$embed->stop;
This method tells mplayer to quit, and resets the widget's internal state.
Before loading another video stream with play(), use this method first.
$embed->tell_slave($something);
This method sends a command to the mplayer slave process. The available commands are documented in the mplayer-slave-spec.txt file in the source distribution.
1. Do something about controlling aspect ration. We need a way to get the aspect ratio of the video stream, and use a Gtk2::AspectFrame to constrain the shape of the widget.
2. Implement more convenience wrappers around the mplayer command set.
2. Implement a stream_ended signal that watches the mplayer process and
emits when it quits.
GStreamer, for a much more powerful, general purpose multimedia system that's compatible with GTK+.
Gavin Brown (gavin dot brown at uk dot com)
(c) 2005 Gavin Brown. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Gtk2-Ex-MPlayerEmbed documentation | Contained in the Gtk2-Ex-MPlayerEmbed distribution. |
# $Id: MPlayerEmbed.pm,v 1.7 2006/01/02 19:44:41 jodrell Exp $ # Copyright (c) 2005 Gavin Brown. All rights reserved. This program is free # software; you can redistribute it and/or modify it under the same terms as # Perl itself. package Gtk2::Ex::MPlayerEmbed; use constant { PAUSE => 'pause', RESUME => 'pause', # 'pause' is really a toggle CLOSE => 'quit', }; use Carp; use FileHandle; use Gtk2; use vars qw($VERSION $STATE_ENUM_PKG); use strict; our $VERSION = '0.02'; BEGIN { our $STATE_ENUM_PKG = sprintf('%s::PlayingState', __PACKAGE__); Glib::Type->register_enum( $STATE_ENUM_PKG, 'stopped', 'playing', 'paused' ); } *new = \&Glib::Object::new;
Glib::Type->register( Gtk2::Socket::, __PACKAGE__, properties => [ Glib::ParamSpec->string( 'mplayer_path', 'MPlayer Path', 'Path to the MPlayer program', '/usr/bin/mplayer', [qw/readable writable/], ), Glib::ParamSpec->string( 'args', 'MPlayer Arguments', 'The arguments supplied to the mplayer command', '', [qw/readable writable/], ), Glib::ParamSpec->boolean( 'loaded', 'Loaded Flag', 'Do we have an input loaded?', 0, [qw/readable writable/], ), Glib::ParamSpec->enum( 'state', 'Playing state', 'The current state of the player', $STATE_ENUM_PKG, 'stopped', [qw/readable writable/], ), ], ); sub INIT_INSTANCE { my $self = shift; $self->modify_bg('normal', Gtk2::Gdk::Color->new(0, 0, 0)); $self->{slave} = FileHandle->new; $self->slave->autoflush(1); return 1; }
sub play { my ($self, $file) = @_; if ($self->get('loaded') && $self->get('state') ne 'stopped') { if ($self->get('state') eq 'playing') { carp("Can't play while still playing, use stop() first."); return undef; } else { return $self->resume; } } else { my $cmd = sprintf( '|%s -slave -wid %d -geometry %dx%d %s "%s" 1>/dev/null 2>/dev/null', $self->get('mplayer_path'), $self->get_id, $self->allocation->width, $self->allocation->height, $self->get('args'), $file ); if (!$self->slave->open($cmd)) { croak("Cannot open '$cmd': $!"); return undef; } else { $self->set('loaded', 1); $self->set('state', 'playing'); return 1; } } }
sub pause { my $self = shift; if (!$self->get('loaded') || $self->get('state') eq 'stopped') { carp("Player must be loaded and playing before it can pause/resume."); return undef; } else { $self->set('state', 'paused'); return $self->tell_slave(PAUSE); } }
sub resume { $_[0]->pause }
sub stop { my $self = shift; if (!$self->get('loaded') || $self->get('state') eq 'stopped') { carp("Can't stop an unloaded or stopped stream."); } else { $self->tell_slave(CLOSE); $self->slave->close; $self->{slave} = FileHandle->new; $self->set('loaded', undef); $self->set('state', 'stopped'); } }
sub tell_slave { my ($self, $msg) = @_; return $self->slave->print("$msg\n"); } sub slave { return $_[0]->{slave}; }
1;