c#Windwosmedia控件使用探索
c#Windwosmedia控件使用探索收藏
一直想自己写一个音乐播放器,原来打算用JAVA中的JMF API,后来发现这个JMF实现的播放器有局限,还不如借助C#的空间,利用VisaulStudio2005来开发.先看效果图
控件的使用主要是使用WINDOWS MEDIA的 API,这里只是简单的使用了他的URL属性,该属性是需要播放文件的位置.下面看代码,其中包含了自己写得一个播放列表功能.
主界面:
播放列表编辑界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 谢飞的专属播放器
{
/// <summary>
/// 播放列表窗口
/// </summary>
public partial class DocumentForm : Form
{
public MainForm mainform;
public DocumentForm(MainForm mainform)
{
InitializeComponent();
this.mainform = mainform;
}
private void btnAdd_Click(object sender, EventArgs e)
{
addListForm listForm = new addListForm(this);
listForm.ShowDialog();
}
private void DocumentForm_Load(object sender, EventArgs e)
{
if (mainform.document != null)
{
List<string> list = mainform.document.getListTitle();
this.lisTitle.Items.AddRange(list.ToArray());
}
}
private void lisTitle_SelectedIndexChanged(object sender, EventArgs e)
{
if (lisTitle.SelectedItem != null)
{
this.lisDetails.Items.Clear();
string title = this.lisTitle.SelectedItem.ToString();
List<string> list = mainform.document.getDetailsByTitle(title);
this.lisDetails.Items.AddRange(list.ToArray());
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDeleteTitle_Click(object sender, EventArgs e)
{
if (lisTitle.SelectedItem != null)
{
if (!("".Equals(lisTitle.SelectedItem.ToString())))
{
lisDetails.Items.Clear();
mainform.document.deleteTitle(lisTitle.SelectedItem.ToString());
lisTitle.Items.Remove(lisTitle.SelectedItem);
}
}
}
private void btnDeleteDetails_Click(object sender, EventArgs e)
{
if (lisDetails.SelectedItem != null)
{
mainform.document.deleteDetails(lisTitle.SelectedItem.ToString(), lisDetails.SelectedItem.ToString());
lisDetails.Items.Remove(lisDetails.SelectedItem);
}
}
private void btnDone_Click(object sender, EventArgs e)
{
mainform.saveList();
mainform.loadList();
this.Close();
}
}
}
播放列表类:
增加 播放列表:
主界面:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization;
- namespace 谢飞的专属播放器
- {
- /// <summary>
- /// 主窗体
- /// </summary>
- public partial class MainForm : Form
- {
- public MyDocument document=new MyDocument();
- public MainForm()
- {
- InitializeComponent();
- }
- public void loadList()
- {
- if (initList())
- {
- this.ManageMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.myDocumentMenuItem });
- List<string> title = document.getListTitle();
- for (int i = 0; i < title.Count; i++)
- {
- ToolStripMenuItem item = new ToolStripMenuItem(title[i]);
- this.ManageMenuItem.DropDownItems.Add(item);
- List<string> details = document.getDetailsByTitle(title[i]);
- for (int j = 0; j < details.Count; j++)
- {
- ToolStripMenuItem detailsItem = new ToolStripMenuItem(details[j]);
- detailsItem.Click += new System.EventHandler(this.detailsItem_Click);
- item.DropDownItems.Add(detailsItem);
- }
- }
- }
- }
- /// <summary>
- /// 保存列表
- /// </summary>
- /// <returns></returns>
- public bool saveList()
- {
- try
- {
- Stream stream = File.Open("list.xml", FileMode.OpenOrCreate);
- BinaryFormatter formator = new BinaryFormatter();
- formator.Serialize(stream, document);
- stream.Close();
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- /// <summary>
- /// 初始化列表
- /// </summary>
- /// <returns></returns>
- public bool initList()
- {
- try
- {
- Stream stream = File.Open("list.xml", FileMode.OpenOrCreate);
- BinaryFormatter formator = new BinaryFormatter();
- document = (MyDocument)formator.Deserialize(stream);
- stream.Close();
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- private void detailsItem_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem item =(ToolStripMenuItem)sender;
- player.URL = item.Text;
- }
- private void openMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog dg = new OpenFileDialog();
- dg.Multiselect = false;
- DialogResult rs = dg.ShowDialog();
- if (rs == DialogResult.OK)
- {
- player.URL = dg.FileName;
- }
- }
- private void aboutMenuItem_Click(object sender, EventArgs e)
- {
- aboutForm aboutF = new aboutForm();
- aboutF.ShowDialog();
- }
- private void myDocumentMenuItem_Click(object sender, EventArgs e)
- {
- DocumentForm docForm = new DocumentForm(this);
- docForm.ShowDialog();
- }
- /// <summary>
- /// 加载的时候初始化列表
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MainForm_Load(object sender, EventArgs e)
- {
- saveList();
- loadList();
- }
- }
- }
播放列表编辑界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 谢飞的专属播放器
{
/// <summary>
/// 播放列表窗口
/// </summary>
public partial class DocumentForm : Form
{
public MainForm mainform;
public DocumentForm(MainForm mainform)
{
InitializeComponent();
this.mainform = mainform;
}
private void btnAdd_Click(object sender, EventArgs e)
{
addListForm listForm = new addListForm(this);
listForm.ShowDialog();
}
private void DocumentForm_Load(object sender, EventArgs e)
{
if (mainform.document != null)
{
List<string> list = mainform.document.getListTitle();
this.lisTitle.Items.AddRange(list.ToArray());
}
}
private void lisTitle_SelectedIndexChanged(object sender, EventArgs e)
{
if (lisTitle.SelectedItem != null)
{
this.lisDetails.Items.Clear();
string title = this.lisTitle.SelectedItem.ToString();
List<string> list = mainform.document.getDetailsByTitle(title);
this.lisDetails.Items.AddRange(list.ToArray());
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDeleteTitle_Click(object sender, EventArgs e)
{
if (lisTitle.SelectedItem != null)
{
if (!("".Equals(lisTitle.SelectedItem.ToString())))
{
lisDetails.Items.Clear();
mainform.document.deleteTitle(lisTitle.SelectedItem.ToString());
lisTitle.Items.Remove(lisTitle.SelectedItem);
}
}
}
private void btnDeleteDetails_Click(object sender, EventArgs e)
{
if (lisDetails.SelectedItem != null)
{
mainform.document.deleteDetails(lisTitle.SelectedItem.ToString(), lisDetails.SelectedItem.ToString());
lisDetails.Items.Remove(lisDetails.SelectedItem);
}
}
private void btnDone_Click(object sender, EventArgs e)
{
mainform.saveList();
mainform.loadList();
this.Close();
}
}
}
播放列表类:
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Text;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace 谢飞的专属播放器
- {
- /// <summary>
- /// 播放列表
- /// </summary>
- [Serializable]
- public class MyDocument
- {
- public Hashtable mylist = new Hashtable();
- /// <summary>
- /// 得到所有列表的标题
- /// </summary>
- /// <returns></returns>
- public List<string> getListTitle()
- {
- if (mylist == null)
- {
- return null;
- }
- List<string> title = new List<string>();
- foreach (DictionaryEntry de in mylist)
- {
- title.Add((string)de.Key);
- }
- return title;
- }
- /// <summary>
- /// 得到制定标题下的详细内容
- /// </summary>
- /// <param name="title"></param>
- /// <returns></returns>
- public List<string> getDetailsByTitle(string title)
- {
- if (mylist == null)
- {
- return null;
- }
- if (mylist.ContainsKey(title))
- {
- foreach (DictionaryEntry de in mylist)
- {
- if (((string)de.Key).Equals(title))
- {
- return (List<string>)de.Value;
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 删除播放列表
- /// </summary>
- /// <param name="title"></param>
- /// <returns></returns>
- public bool deleteTitle(string title)
- {
- try
- {
- mylist.Remove(title);
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- /// <summary>
- /// 删除制定列表的指定内容
- /// </summary>
- /// <param name="title"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool deleteDetails(string title, string value)
- {
- foreach (DictionaryEntry de in mylist)
- {
- if (((string)de.Key).Equals(title))
- {
- List<string> list = (List<string>)de.Value;
- list.Remove(value);
- mylist.Remove(title);
- mylist.Add(title, list);
- return true;
- }
- }
- return false;
- }
- }
- /// <summary>
- /// 播放列表
- /// </summary>
- public class playlist
- {
- private string name;
- private List<string> details;
- public string getName()
- {
- return this.name;
- }
- public List<string> getDetails()
- {
- return this.details;
- }
- public void setName(string name)
- {
- this.name = name;
- }
- public void setDetails(List<string> details)
- {
- this.details = details;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace 谢飞的专属播放器
- {
- /// <summary>
- /// 增加播放列表窗口
- /// </summary>
- public partial class addListForm : Form
- {
- public DocumentForm documentForm;
- public addListForm(DocumentForm documentForm)
- {
- InitializeComponent();
- this.documentForm = documentForm;
- }
- private void btnFile_Click(object sender, EventArgs e)
- {
- OpenFileDialog openDig = new OpenFileDialog();
- DialogResult dg = openDig.ShowDialog();
- if (dg == DialogResult.OK)
- {
- for (int i = 0; i < openDig.FileNames.Length;i++ )
- {
- listDetails.Items.Add(openDig.FileNames[i]);
- }
- }
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (listDetails.SelectedItem != null)
- {
- listDetails.Items.Remove(listDetails.SelectedItem);
- }
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- if ("".Equals(txtTitle.Text) || null == txtTitle.Text)
- {
- MessageBox.Show("列表标题必须填写");
- }
- else
- {
- List<string> list=new List<string>();
- for(int i=0;i<listDetails.Items.Count;i++){
- list.Add(listDetails.Items[i].ToString());
- }
- documentForm.lisTitle.Items.Add(txtTitle.Text);
- documentForm.mainform.document.mylist.Add(txtTitle.Text,list);
- this.Close();
- }
- }
- }
- }