c#Windwosmedia控件使用探索

c#Windwosmedia控件使用探索收藏
一直想自己写一个音乐播放器,原来打算用JAVA中的JMF API,后来发现这个JMF实现的播放器有局限,还不如借助C#的空间,利用VisaulStudio2005来开发.先看效果图
控件的使用主要是使用WINDOWS MEDIA的 API,这里只是简单的使用了他的URL属性,该属性是需要播放文件的位置.下面看代码,其中包含了自己写得一个播放列表功能.
主界面:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Runtime.Serialization;
  11. namespace 谢飞的专属播放器
  12. {
  13.     /// <summary>
  14.     /// 主窗体
  15.     /// </summary>
  16.     public partial class MainForm : Form
  17.     {
  18.         public MyDocument document=new MyDocument();
  19.         public MainForm()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         public void loadList()
  24.         {
  25.             if (initList())
  26.             {
  27.                 this.ManageMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.myDocumentMenuItem });
  28.                 List<string> title = document.getListTitle();
  29.                 for (int i = 0; i < title.Count; i++)
  30.                 {
  31.                     ToolStripMenuItem item = new ToolStripMenuItem(title[i]);
  32.                     this.ManageMenuItem.DropDownItems.Add(item);
  33.                     List<string> details = document.getDetailsByTitle(title[i]);
  34.                     for (int j = 0; j < details.Count; j++)
  35.                     {
  36.                         ToolStripMenuItem detailsItem = new ToolStripMenuItem(details[j]);
  37.                         detailsItem.Click += new System.EventHandler(this.detailsItem_Click);
  38.                         item.DropDownItems.Add(detailsItem);
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.         /// <summary>
  44.         /// 保存列表
  45.         /// </summary>
  46.         /// <returns></returns>
  47.         public bool saveList()
  48.         {
  49.             try
  50.             {
  51.                 Stream stream = File.Open("list.xml", FileMode.OpenOrCreate);
  52.                 BinaryFormatter formator = new BinaryFormatter();
  53.                 formator.Serialize(stream, document);
  54.                 stream.Close();
  55.                 return true;
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 Console.WriteLine(ex.Message);
  60.                 return false;
  61.             }
  62.         }
  63.         /// <summary>
  64.         /// 初始化列表
  65.         /// </summary>
  66.         /// <returns></returns>
  67.         public bool initList()
  68.         {
  69.             try
  70.             {
  71.                 Stream stream = File.Open("list.xml", FileMode.OpenOrCreate);
  72.                 BinaryFormatter formator = new BinaryFormatter();
  73.                 document = (MyDocument)formator.Deserialize(stream);
  74.                 stream.Close();
  75.                 return true;
  76.             }
  77.             catch (Exception ex)
  78.             {
  79.                 Console.WriteLine(ex.Message);
  80.                 return false;
  81.             }
  82.         }
  83.         private void detailsItem_Click(object sender, EventArgs e)
  84.         {
  85.             ToolStripMenuItem item =(ToolStripMenuItem)sender;
  86.             player.URL = item.Text;
  87.         }
  88.         private void openMenuItem_Click(object sender, EventArgs e)
  89.         {
  90.             OpenFileDialog dg = new OpenFileDialog();
  91.             dg.Multiselect = false;
  92.             DialogResult rs = dg.ShowDialog();
  93.             if (rs == DialogResult.OK)
  94.             {
  95.                 player.URL = dg.FileName;
  96.             }
  97.         }
  98.         private void aboutMenuItem_Click(object sender, EventArgs e)
  99.         {
  100.             aboutForm aboutF = new aboutForm();
  101.             aboutF.ShowDialog();
  102.         }
  103.         private void myDocumentMenuItem_Click(object sender, EventArgs e)
  104.         {
  105.             DocumentForm docForm = new DocumentForm(this);
  106.             docForm.ShowDialog();
  107.         }
  108.         /// <summary>
  109.         /// 加载的时候初始化列表
  110.         /// </summary>
  111.         /// <param name="sender"></param>
  112.         /// <param name="e"></param>
  113.         private void MainForm_Load(object sender, EventArgs e)
  114.         {
  115.             saveList();
  116.             loadList();
  117.         }
  118.     }
  119. }

播放列表编辑界面

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();
        }

    }
}
播放列表类:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.IO;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. namespace 谢飞的专属播放器
  9. {
  10.     /// <summary>
  11.     /// 播放列表
  12.     /// </summary>
  13.     [Serializable]
  14.    public class MyDocument
  15.     {
  16.         public Hashtable mylist = new Hashtable();
  17.         /// <summary>
  18.         /// 得到所有列表的标题
  19.         /// </summary>
  20.         /// <returns></returns>
  21.         public List<string> getListTitle()
  22.         {
  23.             if (mylist == null)
  24.             {
  25.                 return null;
  26.             }
  27.             List<string> title = new List<string>();
  28.             foreach (DictionaryEntry de in mylist)
  29.             {
  30.                 title.Add((string)de.Key);
  31.             }
  32.             return title;
  33.         }
  34.         /// <summary>
  35.         /// 得到制定标题下的详细内容
  36.         /// </summary>
  37.         /// <param name="title"></param>
  38.         /// <returns></returns>
  39.         public List<string> getDetailsByTitle(string title)
  40.         {
  41.             if (mylist == null)
  42.             {
  43.                 return null;
  44.             }
  45.             if (mylist.ContainsKey(title))
  46.             {
  47.                 foreach (DictionaryEntry de in mylist)
  48.                 {
  49.                     if (((string)de.Key).Equals(title))
  50.                     {
  51.                         return (List<string>)de.Value;
  52.                     }
  53.                 }
  54.             }
  55.             return null;
  56.         }
  57.         /// <summary>
  58.         /// 删除播放列表
  59.         /// </summary>
  60.         /// <param name="title"></param>
  61.         /// <returns></returns>
  62.        public bool deleteTitle(string title)
  63.        {
  64.            try
  65.            {
  66.                mylist.Remove(title);
  67.                return true;
  68.            }
  69.            catch (Exception ex)
  70.            {
  71.                return false;
  72.            }
  73.        }
  74.         /// <summary>
  75.         /// 删除制定列表的指定内容
  76.         /// </summary>
  77.         /// <param name="title"></param>
  78.         /// <param name="value"></param>
  79.         /// <returns></returns>
  80.        public bool deleteDetails(string title, string value)
  81.        {
  82.            foreach (DictionaryEntry de in mylist)
  83.            {
  84.                if (((string)de.Key).Equals(title))
  85.                {
  86.                    List<string> list = (List<string>)de.Value;
  87.                    list.Remove(value);
  88.                    mylist.Remove(title);
  89.                    mylist.Add(title, list);
  90.                    return true;
  91.                }
  92.            }
  93.            return false;
  94.        }
  95.     }
  96.     /// <summary>
  97.     /// 播放列表
  98.     /// </summary>
  99.     public class playlist
  100.     {
  101.         private string name;
  102.         private List<string> details;
  103.         public string getName()
  104.         {
  105.             return this.name;
  106.         }
  107.         public List<string> getDetails()
  108.         {
  109.             return this.details;
  110.         }
  111.         public void setName(string name)
  112.         {
  113.             this.name = name;
  114.         }
  115.         public void setDetails(List<string> details)
  116.         {
  117.             this.details = details;
  118.         }
  119.     }
  120. }
增加 播放列表:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace 谢飞的专属播放器
  9. {
  10.     /// <summary>
  11.     /// 增加播放列表窗口
  12.     /// </summary>
  13.     public partial class addListForm : Form
  14.     {
  15.         public DocumentForm documentForm;
  16.         public addListForm(DocumentForm documentForm)
  17.         {
  18.             InitializeComponent();
  19.             this.documentForm = documentForm;   
  20.         }
  21.         private void btnFile_Click(object sender, EventArgs e)
  22.         {
  23.             OpenFileDialog openDig = new OpenFileDialog();
  24.             DialogResult dg = openDig.ShowDialog();
  25.             if (dg == DialogResult.OK)
  26.             {
  27.                 for (int i = 0; i < openDig.FileNames.Length;i++ )
  28.                 {
  29.                     listDetails.Items.Add(openDig.FileNames[i]);
  30.                 }
  31.             }
  32.         }
  33.         private void btnDelete_Click(object sender, EventArgs e)
  34.         {
  35.             if (listDetails.SelectedItem != null)
  36.             {
  37.                 listDetails.Items.Remove(listDetails.SelectedItem);
  38.             }
  39.         }
  40.         private void btnCancel_Click(object sender, EventArgs e)
  41.         {
  42.             this.Close();
  43.         }
  44.         private void btnAdd_Click(object sender, EventArgs e)
  45.         {
  46.             if ("".Equals(txtTitle.Text) || null == txtTitle.Text)
  47.             {
  48.                 MessageBox.Show("列表标题必须填写");
  49.             }
  50.             else
  51.             {
  52.                 List<string> list=new List<string>();
  53.                 for(int i=0;i<listDetails.Items.Count;i++){
  54.                     list.Add(listDetails.Items[i].ToString());
  55.                 }
  56.                 documentForm.lisTitle.Items.Add(txtTitle.Text);
  57.                 documentForm.mainform.document.mylist.Add(txtTitle.Text,list);
  58.                 this.Close();
  59.             }
  60.         }
  61.     }
  62. }

原文地址:https://www.cnblogs.com/xianyin05/p/1449272.html