zoukankan      html  css  js  c++  java
  • 【wpf基础】wpf MediaElement全屏播放视频功能

     最近在研究如何将视频全屏播放,一开始思路A:弹窗将MediaElement对象add到一个新的全屏窗体,报错

    指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。

    后续转换思路B:将本窗体其他控件隐藏掉,然后窗体最大化,去掉边框,然后把MediaElement设置成屏幕的宽高。

     点击【播放】,加载视频

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            MediaElement myPlayer = new MediaElement();
    
            public MainWindow()
            {
                InitializeComponent();
    
                myPlayer.Margin = new Thickness(1, 1, 1, 1);
                myPlayer.Width = ActualWidth;
                myPlayer.Height = ActualHeight;
    
                myPlayer.LoadedBehavior = MediaState.Manual;
                var mp4_path = AppDomain.CurrentDomain.BaseDirectory + "video.mp4";
                myPlayer.Source = new Uri(mp4_path, UriKind.RelativeOrAbsolute);
    
                (Content as Grid).Children.Add(myPlayer);
            }
    
            void myContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                if (FullScreenHelper.IsFullscreen(this))
                    FullScreenHelper.ExitFullscreen(this);
                else
                    FullScreenHelper.GoFullscreen(this);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                myPlayer.Play();
            }
    
            private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                myPlayer.Width = ActualWidth;
                myPlayer.Height = ActualHeight;
            }
    
        }
    }
    View Code

     双击视频,全屏播放

    有需要这个效果的可以参考

    源码:http://pan.baidu.com/s/1mi8qII8

  • 相关阅读:
    web页面静态化与伪静态化
    mysql 优化之空间换时间
    QPS、PV、UV、RT 之间的关系
    接口
    MySQL 索引
    名词解释
    go 语言标识符
    Git版本控制与工作流
    Maven安装与配置
    IDEA工具使用说明
  • 原文地址:https://www.cnblogs.com/jhli/p/6195434.html
Copyright © 2011-2022 走看看