c# - XAML主窗口上的VideoDrawing?
问题描述
我在 WPF 中有以下简单示例来使用 VideoDrawing 对象播放视频文件 - 这是背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MediaTimeline timeline = new MediaTimeline(new Uri(@"c:\test\RedRock-uhd-h264.mp4", UriKind.Absolute));
timeline.RepeatBehavior = RepeatBehavior.Forever;
MediaClock clock = timeline.CreateClock();
MediaPlayer player = new MediaPlayer();
player.Clock = clock;
VideoDrawing drawing = new VideoDrawing();
drawing.Rect = new Rect(0, 0, 820, 600); //<--video size is 620 x 400 same as XAML MainWindow size
drawing.Rect = new Rect(0, 0, 420, 280); //<--video size is 620 x 400 same as XAML MainWindow size
drawing.Rect = new Rect(0, 0, 220, 80); //<--video size is 620 x 400 same as XAML MainWindow size
drawing.Rect = new Rect(0, 0, 1, 1); //<--video size is 620 x 400 same as XAML MainWindow size
drawing.Rect = new Rect(0, 0, 0, 0); //<--video does not show
//drawing.Rect = new Rect(0, 0, 0, 0); //<--video does not show
drawing.Player = player;
DrawingBrush brush = new DrawingBrush(drawing);
this.Background = brush;
}
}
这是 XAML:
<Window x:Class="MyMediaPlayer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MediaPlayer in WPF" Width="620" Height="400"
WindowStyle="None"
ShowInTaskbar="True"
AllowsTransparency="True"
Background="Transparent"
WindowStartupLocation="Manual"
Left="0"
Top="0">
</Window>
查看上面的“drawing.Rect = new Rect(...)”行并注意注释 - 无论我将 Rect 设置为什么大小 - 视频始终以 XAML MainWindow 大小(620、400)的大小播放,但是我必须设置至少一些 Rect 大小我不能将其设置为 0 或将其注释掉。似乎视频应该以设置的 Rect 大小播放,除非它大于 XAML MainWindow?这是什么我没有不了解我在做什么,为什么视频不能播放到 Rect 的大小?
解决方案
将拉伸模式设置为无:
brush.Stretch = Stretch.None;
当然,问题在于您现在无法设置播放器周围区域的颜色。如果您想控制它,则必须切换到 VisualBrush 并改用 MediaElement:
// create a grid and bind it to the parent window's size
var grid = new Grid { Background = Brushes.CornflowerBlue }; // <- sets background color
grid.SetBinding(WidthProperty, new Binding
{
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
Path = new PropertyPath("ActualWidth"),
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
grid.SetBinding(HeightProperty, new Binding
{
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
Path = new PropertyPath("ActualHeight"),
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
// add the media player
grid.Children.Add(new MediaElement
{
Source = new Uri("yourvideo.mp4", UriKind.RelativeOrAbsolute),
LoadedBehavior = MediaState.Play,
Stretch = Stretch.Fill,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Width = 640, // <-- video size
Height = 480
});
// wrap it all up in a visual brush
this.Background = new VisualBrush { Visual = grid };
推荐阅读
- android - 当我使用已弃用的 api 时,Android Studio 3.1.3 不显示任何错误
- java - 使用 slf4j + log4j 和 dropwizard
- python - python:无法打开文件“帮助”:[Errno 2]没有这样的文件或目录
- audio-processing - librosa中音频样本的单位是什么?
- function - Sass Function with list as argument
- java - 2的幂?代码有什么问题
- angular - 清晰的隐藏警报计时器
- linux - 将多个文件更改为可执行简化的脚本
- php - 如何使用 PHP 代码同时获取 IPv4 和 IPv6 地址?
- python - Singleton 不在 Cython 中工作