XAML主窗口上的VideoDrawing?

时间:2019-01-04 22:57:19

标签: c# wpf media-player

我在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的大小?

1 个答案:

答案 0 :(得分:1)

将拉伸模式设置为“无”:

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 };