Skip to content

Use VlcPlayer with other controls

HIGAN edited this page Mar 28, 2016 · 2 revisions

VlcPlayer 提供很多方式用于呈现视频,而 VlcPlayer 控件是一个经过多重优化的呈现视频的方式,如果没有特殊需求,我们强烈推荐使用 VlcPlayer 来呈现视频图像,VlcPlayer 支持动态修正 DAR 与 SAR,这可能改善某些视频的显示比例错误问题(#32),并且 VlcPlayer 还支持与 DVD 菜单的鼠标交互(#5),当然我们仍然提供通过其他控件来呈现视频的方法,这可能有利于在基于 MVVM 的应用中播放视频。

VlcPlayer provide many ways to display a video, and VlcPlayer control is one of those through multiple Optimization. If there is no special requirement, we strongly recommend the use of VlcPlayer to render video image. VlcPlayer supports dynamic revised SAR and DAR, this may improve some aspect ratio video error problem (#32). And VlcPlayer supports mouse interaction with the DVD menu (#5). Of course, we still provided by other control methods to render the video, which may be beneficial in MVVM-based application.

Create VlcPlayer in C# code.

本小节将讲述如何在 C# 代码中新建 VlcPlayer。 This section describes how to create VlcPlayer in C # code.

如果是在 C# 代码中使用 VlcPlayer,而且不准备将控件加入到视觉树上,请使用new VlcPlayer(bool)或者new VlcPlayer(Dispatcher)来创建 VlcPlayer。
If you want to use VlcPlayer in C# code, and will not add it to visual tree, use new VlcPlayer(bool) or new VlcPlayer(Dispatcher) to create VlcPlayer.

// 使用 new VlcPlayer(bool) 来创建 VlcPlayer。
// Use "new VlcPlayer(bool)" to create a VlcPlayer.

var player = new VlcPlayer(true);            // 将在 ThreadSeparatedImage 控件中使用 VlcPlayer。       Use VlcPlayer with ThreadSeparatedImage control.
// var player = new VlcPlayer(false);        // 将在其他运行在默认 UI 线程的控件中使用 VlcPlayer。       Use VlcPlayer with other control which running on default UI thread.
player.Initialize(vlcPath, vlcOptions);
// 使用 new VlcPlayer(Dispatcher) 来创建 VlcPlayer。
// Use "new VlcPlayer(Dispatcher)" to create a VlcPlayer.

var player = new VlcPlayer(videoDisplayControl.Dispatcher); // 将在 videoDisplayControl 中使用 VlcPlayer。            Use VlcPlayer with "videoDisplayControl".
//var player = new VlcPlayer(null);                         // 将在其他运行在默认 UI 线程的控件中使用 VlcPlayer。       Use VlcPlayer with "videoDisplayControl".
player.Initialize(vlcPath, vlcOptions);

Display video with Image control.

本小节将描述如何使用 Image 控件来呈现视频。
This section describes how to display video with Image control.

以下代码在后台代码创建了一个 VlcPlayer 并将 VideoSource 提供给 Image 控件。
Those code create a new VlcPlayer in C# code, and set VideoSource to Image control.

var player = new VlcPlayer(imageControl.Dispatcher);       // 创建 VlcPlayer 并且设置呈现线程为 Image 控件的线程。        Create VlcPlayer and set display thread to Image control thread.
//var player = new VlcPlayer(false);                       // 创建 VlcPlayer 并且设置呈现线程为默认 UI 线程。             Create VlcPlayer and set display thread to default UI thread.

player.Initialize(vlcPath, vlcOptions);

imageControl.Source = player.VideoSource;
player.VideoSourceChanged += (o, args) =>
{
    imageControl.Source = args.NewVideoSource;             // 更新 VideoSource。                                        Update VideoSource.
};

Display video with ThreadSeparatedImage control.

本小节将描述如何使用 ThreadSeparatedImage 来呈现视频。
This section describes how to display video with ThreadSeparatedImage control.

ThreadSeparatedImage 控件是对 Image 控件的封装,将 Image 放置在一个独立的线程上,所有 ThreadSeparatedImage 控件内部的 Image 控件都是运行在该独立线程上。 ThreadSeparatedImage control is a wrapper of Image control, it put Image in a separat thread, all internal Image of ThreadSeparatedImage is running on this thread.

以下代码在后台代码创建了一个 VlcPlayer 并将 VideoSource 提供给 ThreadSeparatedImage 控件。
Those code create a new VlcPlayer in C# code, and set VideoSource to ThreadSeparatedImage control.

var player = new VlcPlayer(true);                         // 创建 VlcPlayer 并且设置呈现线程为 ThreadSeparatedImage 公用线程。         Create VlcPlayer and set display thread to ThreadSeparatedImage common thread.

player.Initialize(vlcPath, vlcOptions);

threadSeparatedImage.Source = player.VideoSource;
player.VideoSourceChanged += (o, args) =>
{
    threadSeparatedImage.Source = args.NewVideoSource;    // 更新 VideoSource。                                                      Update VideoSource.
};