hudec117 / Mpv.NET-lib-

.NET embeddable video/media player based on mpv for WinForms and WPF

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adding new function to mpv

leichenxu opened this issue · comments

hi, i am a beginner user in c#, and i am trying to add functions for play next frame and previous frame in your mpv code, is there anyway to do it? sorry for my bad english

Hello @leichenxu, yes there is.
In the mpv manual there are 2 commands, frame-step and frame-back-step. These are not implemented at the moment but you're more than welcome to try.

Use this existing method as a point of reference on how you should implement it in the MpvPlayer class.

If you need more guidance, please reach out.

ok, thank you very much, i will try it

hi again, thank you very much, i just use mpv.Command("frame-step") and actually worked, thanks

Hello @leichenxu I will be releasing 1.1.0 soon. I will implement frame stepping methods into this release.

ok, thanks for the help, and i have something to say if this may help you in your new version,

/// Show next frame.
/// </summary>
public void NextFrame()
{
    lock (mpvLock)
    {
        mpv.Command("frame-step");
        IsPlaying = false;
    }
}

/// <summary>
/// Show back frame.
/// </summary>
public void BackFrame()
{
    lock (mpvLock)
    {
        mpv.Command("frame-back-step");
        IsPlaying = false;
    }
}

i used this two simply methods for go to next and previous frame, and sometimes, this have fails, for verify why fail, i write this in your code

public void Command(params string[] args)
{
    Guard.AgainstDisposed(disposed, nameof(Mpv));
    Guard.AgainstNull(args, nameof(args));

    if (args.Length < 1)
        throw new ArgumentException("Missing arguments.", nameof(args));

    var argsPtr = MpvMarshal.GetComPtrForManagedUTF8StringArray(args, out IntPtr[] argsPtrs);
    if (argsPtr == IntPtr.Zero)
        throw new MpvAPIException("Failed to convert string array to pointer array.");

    //show all command used
    foreach (String s in args)
    {
        Console.WriteLine(s);
    }

    try
    {
        var error = Functions.Command(Handle, argsPtr);
        if (error != MpvError.Success)
            throw MpvAPIException.FromError(error, Functions);
    }catch (Exception e){
        //show in console when fail
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("Command failed");
        Console.WriteLine(e);
        foreach (String s in args)
        {
            Console.WriteLine(s);
        }
        Console.WriteLine("-----------------------------------");               
    }
    finally
    {
        MpvMarshal.FreeComPtrArray(argsPtrs);
        Marshal.FreeCoTaskMem(argsPtr);
    }
}

and i notify, that sometimes, when use NextFrame() and BackFrame(), continuously, have fail, i can not discover the reason, just for say it.

EDIT BY @hudec117: formatted code

@leichenxu frame stepping added in version 1.1.0