The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MPV::Simple::Pipe

SYNOPSIS

    use strict;
    use warnings;
    use utf8;
    use MPV::Simple::Pipe;
    use Tcl::Tk;
    
    # 1) It is recommended to to create the MPV::Simple::Pipe object before TCL
    # interpreter even if it seems not as necessary as in MPV::Simple::JSON
    # 2) If you want to handle events you have to pass a true value to the 
    # option event_handling 
    my $mpv = MPV::Simple::Pipe->new(event_handling => 1);
    
    my $int = Tcl::Tk->new();
    my $mw = $int->mainwindow();
    $mw->title("MPV::Simple example");  
    
    # Create the video frame
    my $f = $mw->Frame(-width => 640, -height => 480)->pack(-expand =>1,-fill => "both");
    
    $mpv->initialize();
    
    # With the MPV property "wid" you can embed MPV in a foreign window
    $mpv->set_property_string("wid",$f->id());
    
    # The video shall start paused here
    $mpv->set_property_string('pause','yes');
    
    # Load a video file
    $mpv->command("loadfile", "path_to_video.ogg");
    
    # For handling events you must repeatly call a event handler.
    # I think, it is enough to call the event handler every 500/1000ms
    $int->call('after',1000,\&handle_events);
    
    my $b1 = $mw->Button(
        -text   =>  "Play",
        -command => sub {$mpv->set_property_string('pause','no')}
    )->pack(-side => 'left');
    my $b2 = $mw->Button(
        -text   =>  "Pause",
        -command => sub {$mpv->set_property_string('pause','yes')}
    )->pack(-side => 'left');
    my $b3 = $mw->Button(
        -text   =>  "Backward",
        -command => sub {$mpv->command('seek',-5)}
    )->pack(-side => 'left');
    my $b4 = $mw->Button(
        -text   =>  "Forward",
        -command => sub {$mpv->command('seek',5)}
    )->pack(-side => 'left');
    my $b5 = $mw->Button(
        -text   =>  "Close",
        -command => sub {$mpv->terminate_destroy();$mw->destroy();}
    )->pack(-side => 'left');
    $int->MainLoop;
    
    # Event handler
    # If you set $opt{event_handling} to a true value in the constructor
    # the events are sent through a non-blocking pipe ($mpv->{evreader}) you can access 
    # by the method $mpv->get_events(); which returns a hashref of the event
    # The event_ids can be translated to the event names with the global array 
    # $MPV::Simple::event_names[$id]
    sub handle_events {
        while ( my $event = $mpv->get_events() ) {
            if ($event->{event} eq "property-change") {
                    print "prop ".$event->{name}." changed to ".$event->{data}." %\n";
            }
            else {
                    print $event->{event}."\n";
            }
        }
    
    # Don't forget to call the event handler repeatly
    $int->call('after',1000,\&handle_events);
    }
    

DESCRIPTION

Using MPV::Simple as a seperate process to integrate it in a foreign event loop, especially to interact with GUI toolkits. The module give access to the same methods as MPV::Simple. Furthermore, if the option $opt{event_handling} is passed to a true value, events are passed trough a pipe ($mpv->{evreader}) which can be accessed by $mpv->get_events(). In this case you can and must handle the events by a repeatly call of a subroutine. See the example above.

Methods

The following methods exist. See MPV::Simple for a detailled description.

=item* my $mpv = MPV::Simple->new()

=item* $mpv->initialize()

=item* $mpv->set_property_string('name','value');

=item* $mpv->get_property_string('name');

=item* $mpv->observe_property_string('name', id);

=item* $mpv->unobserve_property(registered_id);

=item* $mpv->command($command, @args);

=item* $mpv->terminate_destroy() Note: After terminating you cannot use the MPV object anymore. Instead you have to create a new MPV object.

Error handling

You can use MPV::Simple::error_names(), MPV::Simple::check_error() and MPV::Simple::warn_error() to handle errors. See MPV::Simple for details.

SEE ALSO

See the doxygen documentation at https://github.com/mpv-player/mpv/blob/master/libmpv/client.h and the manual of the mpv media player in http://mpv.io.