如何从C#WPF中的不同窗口控制libvlc dotnet [英] How to control libvlc dotnet from a different window in C# WPF

查看:217
本文介绍了如何从C#WPF中的不同窗口控制libvlc dotnet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮忙吗?我已经摸不着头几天了!



我正在尝试创建一个可以在第二个屏幕上播放视频的应用,但是播放/暂停/停止主监视器上的控件和进度条。



我正在使用带有vlc.dotnet包装器的LibVLC库。



请原谅任何草率和复制/粘贴的代码,但我现在还在让我们去试试阶段。



这是我对第二个屏幕窗口后面的代码所做的(直接从vlc.dotnet代码示例中提取):



  public   void 选择( String  ytURL , String  ytControl)
{
if (ytURL ==
{
ytURL = http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov;
}

if (ytControl == 播放
{
ytPlayVideo(ytURL);
}
else if (ytControl == 停止
{
// ???
}
else if (ytControl == 暂停
{
// ???
}
}

private void ytPlayVideo( String playURL )
{
if IntPtr .Size == 4
{
// 使用32位库
.MyControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, libvlc-x86));
}
其他
{
// < span class =code-comment>使用64位库
.MyControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, libvlc-x64 ));
}

var options = new string []
{
// VLC选项可以在这里给出请参阅VLC命令行文档。
- fullscreen
};

this .MyControl.MediaPlayer.VlcMediaplayerOptions = options;

// 加载libvlc库并初始化东西。重要的是在调用此方法之前给出选项(如果要传递任何选项)和lib目录。
.MyControl。 MediaPlayer.EndInit();
this .MyControl.MediaPlayer.Play(playURL);
}





这是从控制窗口调用视频窗口的代码片段:



 YTPlayer YTwin =  new  YTPlayer(); 
int s2 = Screen.AllScreens.Count();
if (s2 > 1
{
屏幕s1 = Screen.AllScreens [ 1 ];
System.Drawing.Rectangle r1 = s1.WorkingArea;
YTwin.WindowState = System.Windows.WindowState.Normal;
YTwin.WindowStartupLocation = WindowStartupLocation.Manual;
YTwin.Top = r1.Top;
YTwin.Left = r1.Left;

YTwin.Show();
}
YTwin.Select( 播放);





到现在为止还挺好。这完全播放了视频,我甚至可以在实例化新窗口时将不同的视频链接传递给它,但我不能在我的生活中解决如何在第二个屏幕上将命令传递给VLC WPF控件的情况。已经运行暂停,停止等,或者将数据从视频窗口传回到进度条的控制器窗口。



必须完成两个独立的窗户,因为辅助屏幕需要最大化,而主要的控制表面需要重新调整大小。



我尝试过什么:



我在这里和其他论坛上检查了很多关于如何在另一个窗口中解决WPF控件的论坛,以及如何在类之间传递变量。



例如,提出自定义事件并在实例化时公开它:



 YTPlayer YTwin =  new  YTPlayer(); 
YTwin.RaiseCustomEvent + = new EventHandler< CustomEventArgs>(YTwin_RaiseCustomEvent);





with:



  public   event  EventHandler< CustomEventArgs> RaiseCustomEvent; 





问题是,无论我尝试什么,都会引发vlc.dotnet选择的错误,并说它没有存在。



当YTPlayer窗口打开并运行时,我需要能够从YTControl窗口调用YTPlayer.Select(),而无需实例化YTPlayer的新实例。 br />


有什么想法吗?

解决方案

好的......我明白了!我很厚呵呵呵



我突然意识到(呃!)选择是YTPlayer背后的代码中的一种方法,而不是API中的函数。



我只需要使用:



  foreach (窗口 System.Windows.Application.Current.Windows中)
{
if (window.GetType()== typeof (YTPlayer))
{
...
YTControl中的


,然后在那里移动所有Play / Pause / Stop命令,直接通过Control in来寻址VLC API YTPlayer WPF窗口。


Please can anyone help? I've been scratching my head for days!

I'm trying to create an app which will play video on a second screen, but with the Play/Pause/Stop controls and the progress bar on the primary monitor.

I'm using the LibVLC library with the vlc.dotnet wrapper.

Please excuse any sloppy and copy/pasted code, but I'm still at the "Let's give it a go" stage at the moment.

Here's what I have for the code behind the second screen window (pretty much lifted directly from the vlc.dotnet code sample):

public void Select(String ytURL, String ytControl)
        {
            if (ytURL == "")
            {
                ytURL = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov";
            }

            if (ytControl == "Play")
            {
                ytPlayVideo(ytURL);
            }
            else if (ytControl == "Stop")
            {
                // ???
            }
            else if (ytControl == "Pause")
            {
                // ???
            }
        }

private void ytPlayVideo(String playURL)
        {
            if (IntPtr.Size == 4)
            {
                // Use 32 bits library
                this.MyControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "libvlc-x86"));
            }
            else
            {
                // Use 64 bits library
                this.MyControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "libvlc-x64"));
            }

            var options = new string[]
            {
                // VLC options can be given here. Please refer to the VLC command line documentation.
                "--fullscreen"
            };

            this.MyControl.MediaPlayer.VlcMediaplayerOptions = options;

            // Load libvlc libraries and initializes stuff. It is important that the options (if you want to pass any) and lib directory are given before calling this method.
            this.MyControl.MediaPlayer.EndInit();
            this.MyControl.MediaPlayer.Play(playURL);
        }



And here is the code clip which calls the video window from the control window:

YTPlayer YTwin = new YTPlayer();
            int s2 = Screen.AllScreens.Count();
            if (s2 > 1)
            {
                Screen s1 = Screen.AllScreens[1];
                System.Drawing.Rectangle r1 = s1.WorkingArea;
                YTwin.WindowState = System.Windows.WindowState.Normal;
                YTwin.WindowStartupLocation = WindowStartupLocation.Manual;
                YTwin.Top = r1.Top;
                YTwin.Left = r1.Left;

                YTwin.Show();
            }
            YTwin.Select("", "Play");



So far so good. That plays the video perfectly, and I can even pass different video links to it when the new window is instantiated, but I can't for the life of me work out how to pass commands to the VLC WPF control on the second screen once it's already running for Pause, Stop, etc., or pass the data back from the video window to the controller window for the progress bar.

It has to be done with two separate windows, as the secondary screen needs to be maximised, whilst the control surface on the primary needs to be re-sizeable.

What I have tried:

I've checked out quite a few threads on here and other fora about how to address WPF controls in one window from another, and how to pass variables between classes.

For example, raising a custom event and exposing it at instantiation:

YTPlayer YTwin = new YTPlayer();
YTwin.RaiseCustomEvent += new EventHandler<CustomEventArgs>(YTwin_RaiseCustomEvent);



with:

public event EventHandler<CustomEventArgs> RaiseCustomEvent;



Problem is that whatever I try like that throws an error with the vlc.dotnet "Select" and says it doesn't exist.

I need to be able to call YTPlayer.Select() from YTControl window while YTPlayer window is open and running, without instantiating a new instance of YTPlayer.

Any ideas?

解决方案

OK... I figured it out! I was being thick. Hehehe

I suddenly realised (duh!) that "Select" is a method in the code behind YTPlayer, not a function in the API.

I just had to use:

foreach (Window window in System.Windows.Application.Current.Windows)
                    {
                        if (window.GetType() == typeof(YTPlayer))
{
...
}



in YTControl, then move all of the Play/Pause/Stop commands in there, directly addressing the VLC API through the Control in the YTPlayer WPF window.


这篇关于如何从C#WPF中的不同窗口控制libvlc dotnet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆