在当前屏幕的顶部播放视频

时间:2016-08-19 14:26:13

标签: c# wpf video wpf-controls

我想在当前窗口的顶部播放视频。另一个视频正在当前窗口的背景中播放。

我在WPF项目中使用MMVM和Microsoft的Microsoft.Practices.Unity。我添加了Canvas并在代码中使用MediaPlayerVideoDrawingDrawingBrush来展示播放器Canvas并播放视频。

大部分时间它都能完美运行,但有时它会停止播放视频并显示空白屏幕。

预期:

enter image description here

意外:

enter image description here

用户控件

<UserControl x:Class="VideoDemo.Views.SampleView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:VideoDemo.Views"
         mc:Ignorable="d" 
         >

    <StackPanel Orientation="Horizontal">
    <Canvas x:Name="MainCanvas" Width="540" Height="340">
        <MediaElement Width="540" Height="340" Stretch="Fill">
            <MediaElement.Triggers>
                <EventTrigger RoutedEvent="Window.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <MediaTimeline x:Name="bgVideo" Source="BGLoop.mp4" RepeatBehavior="Forever"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </MediaElement.Triggers>
        </MediaElement>

        <Button Content="Show Popup" Foreground="Black" Click="Button_Click" ></Button>

        <!--Video Player-->

        <Canvas Name="canvasPlayer" Width="540" Height="292"
                Margin="0"                    
                VerticalAlignment="Top"
                >
        </Canvas>
    </Canvas>

</StackPanel>

按钮点击

        private void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Canvas.SetZIndex(canvasPlayer, 99);
            bool isMediaClosed = false;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(10);

            MediaPlayer mp = new MediaPlayer();
            var bgVideoPath = Environment.CurrentDirectory + @"\Touchdown.mp4";
            mp.Open(new Uri(bgVideoPath));

            VideoDrawing vd = new VideoDrawing();
            vd.Player = mp;
            vd.Rect = new Rect(0, 0, 100, 100);

            DrawingBrush db = new DrawingBrush(vd);

            var btnBg = MainCanvas.Background;

            // Affect the drawing brush as a background for all 9 buttons
            //foreach (Button b in videoGrid.Children)
            canvasPlayer.Background = db;

            // Lecture!
            mp.Play();
            timer.Start();

            //Timer to remove the blank screen. Otherwise whole time blank screen sticked
            timer.Tick += (t, l) =>
            {
                timer.Stop();
                if (!isMediaClosed)
                {
                    mp.Stop();
                    mp.Close();

                    canvasPlayer.Background = btnBg;
                    Canvas.SetZIndex(canvasPlayer, -1);
                    isMediaClosed = true;
                }
            };

            mp.MediaEnded += (k, p) =>
            {
                mp.Stop();
                mp.Close();

                canvasPlayer.Background = btnBg;
                Canvas.SetZIndex(canvasPlayer, -1);
                isMediaClosed = true;
            };

        }
        catch (Exception)
        {
        }
    }

对于那些可以下载并检查我的代码的人。我为此编写了一个示例应用程序,您可以从here下载它。 我试图在这个演示应用程序中重新生成此问题,但没有得到。

0 个答案:

没有答案