本文共 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 泗阳行政服务中心{ ///上面的cs代码中所用的ApplicationPath类代码如下:/// 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(); } //存储播放列表中视频的名称 ListfileNames = 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); } }}
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/