博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放)
阅读量:2445 次
发布时间:2019-05-10

本文共 6746 字,大约阅读时间需要 22 分钟。

软件概览:

区域组成:一个MediaElement控件+一个ListView控件+一个TextBlock控件+一个Slider控件+若干button控件

功能介绍:

1、视频区

       用于播放列表中的视频内容

2、控制区

      包含播放(暂停)、停止、快进、快退、音量;返回按钮为关闭此窗体

3、播放列表

      显示所要播放的视频列表,列表中的内容位于一个盘符目录中,向此目录添加视频时,启动此程序,列表将显示此目录下的所有视频,程序已设为播放列表循环播放(非单个循环)。

代码如下:

Xaml:

cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Shapes;using System.IO;namespace 泗阳行政服务中心{    ///     /// VentureNewsWindow.xaml 的交互逻辑    ///     public partial class VentureNewsWindow : Window    {        string root = "", pathMedia = "";        public VentureNewsWindow()        {            InitializeComponent();            InitPath();            AddItemToListView();        }        //获取视频目录        private void InitPath()        {            ApplicationPath ap = new ApplicationPath();            root = ap.GetRtfPath("root");            pathMedia = root + ap.GetRtfPath("pathMedia");        }        //将视频目录下的视频名称添加到ListView中        private void AddItemToListView()        {            string[] files = Directory.GetFiles(pathMedia);            foreach (string file in files)            {                this.listView1.Items.Add(file.Substring(file.LastIndexOf('\\') + 1));            }        }        //窗体加载时调用视频,进行播放        private void Window_Loaded(object sender, RoutedEventArgs e)        {            MediaElementControl();        }        //存储播放列表中视频的名称        List
fileNames = new List
(); private void MediaElementControl() { this.mediaElement1.LoadedBehavior = MediaState.Manual; string[] files = Directory.GetFiles(pathMedia); foreach(string file in files) { fileNames.Add(file.Substring(file.LastIndexOf('\\') + 1)); } this.mediaElement1.Source = new Uri(files[0]); this.mediaElement1.Play(); } //视频播放结束事件 private void mediaElement1_MediaEnded(object sender, RoutedEventArgs e) { //获取当前播放视频的名称(格式为:xxx.wmv) string path = this.mediaElement1.Source.LocalPath; string currentfileName = path.Substring(path.LastIndexOf('\\') + 1); //对比名称列表,如果相同,则播放下一个,如果播放的是最后一个,则从第一个重新开始播放 for (int i = 0; i < fileNames.Count;i++ ) { if (currentfileName == fileNames[i]) { if (i == fileNames.Count - 1) { this.mediaElement1.Source = new Uri(pathMedia + "//" + fileNames[0]); this.mediaElement1.Play(); } else { this.mediaElement1.Source = new Uri(pathMedia + "//" + fileNames[i + 1]); this.mediaElement1.Play(); } break; } } } //播放列表选择时播放对应视频 private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e) { string fileName = this.listView1.SelectedValue.ToString(); this.mediaElement1.Source = new Uri(pathMedia + "//" + fileName); this.mediaElement1.Play(); } //返回按钮 private void button1_Click(object sender, RoutedEventArgs e) { this.Close(); } private void button3_Click(object sender, RoutedEventArgs e) { this.mediaElement1.Position = this.mediaElement1.Position + TimeSpan.FromSeconds(20); } //播放、暂停按钮 private void button3_Click_1(object sender, RoutedEventArgs e) { if (button3.Content.ToString() == "播 放") { mediaElement1.Play(); button3.Content = "暂 停"; mediaElement1.ToolTip = "Click to Pause"; } else { mediaElement1.Pause(); button3.Content = "播 放"; mediaElement1.ToolTip = "Click to Play"; } } //停止播放视频 private void button4_Click(object sender, RoutedEventArgs e) { this.mediaElement1.Stop(); } //快进 private void button5_Click(object sender, RoutedEventArgs e) { mediaElement1.Position = mediaElement1.Position + TimeSpan.FromSeconds(10); } //快退 private void button6_Click(object sender, RoutedEventArgs e) { mediaElement1.Position = mediaElement1.Position - TimeSpan.FromSeconds(10); } }}
上面的cs代码中所用的ApplicationPath类代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;namespace 泗阳行政服务中心{    class ApplicationPath    {        private readonly static string ImagePath = Environment.CurrentDirectory + "\\image\\";        private readonly static string RtfPath = Environment.CurrentDirectory + "\\rtf\\";        XmlDocument xDoc = new XmlDocument();        public ApplicationPath()        {            xDoc.Load(getAppConfigPath());        }        public static string getImagePath()        {            return ImagePath;        }        public static string getRtfPath()        {            return RtfPath;        }        public string getAppConfigPath()        {            return Environment.CurrentDirectory + "\\App.config";        }        //获取App.config值        public string GetRtfPath(string appKey)        {            try            {                XmlNode xNode;                XmlElement xElem;                xNode = xDoc.SelectSingleNode("//appSettings");                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");                if (xElem != null)                {                    return xElem.GetAttribute("value");                }                else                    return "";            }            catch (Exception)            {                return "";            }        }    }}
上面代码中的App.config文件位于项目的/bin/Debug下,内容格式为:

转载地址:http://wrxqb.baihongyu.com/

你可能感兴趣的文章
PostgreSQL中的锁:2.行级锁
查看>>
notebooks_.NET Core与Jupyter Notebooks预览1
查看>>
android 编程app_2020年用于Android App开发的顶级编程语言
查看>>
pvs-stdio ue4_华为云:如今PVS-Studio多云
查看>>
vc编程查找计算机运行记录_如何查找计算机的正常运行时间和安装日期
查看>>
linux下用who查询_如何在没有电缆的情况下流播Who Who
查看>>
mac iphone 备份_如何在Mac上删除iPhone和iPad备份
查看>>
Xion具有绝佳的听起来音乐和皮肤功能
查看>>
psa3尺寸怎么设置_PSA:请勿下载伪造的“ Amazon Alexa设置” iOS应用程序
查看>>
evernote 论文_如何在没有RealPlayer的情况下在Evernote中播放录制的移动音频(.AMR)...
查看>>
synctoy 自动运行_安排SyncToy在Windows 7中使用Task Scheduler自动运行
查看>>
Internet Explorer 9屏幕快照之旅:它是一个全新的界面
查看>>
如何通过调整,附加组件和移动应用程序来增强您的SABnzbd体验
查看>>
mac已安装flash控件_如何在Mac上安装和更新Flash
查看>>
如何删除高级病毒删除程序和其他恶意/欺诈性防病毒恶意软件
查看>>
三星Galaxy S20:打开手势并更改导航栏按钮顺序
查看>>
如何在“动物穿越:新视野”中玩沙发合作游戏(使用一个Switch Console)
查看>>
创建键盘快捷方式以访问隐藏的桌面图标和文件
查看>>
在Windows 7 Media Player中轻松播放Flac,Ogg和其他文件格式
查看>>
evernote离线使用_使用Evernote的Secret Debug菜单优化和加快搜索速度
查看>>