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;
- namespace 谢飞的专属播放器
- {
-
-
-
- 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();
- }
- }
- }
- }