welcome to Qijie's Blog 薛其杰

 

 

代码
  1 ///the most important thing is to reference the MediaPlayer in COM Tab
  2 ///qixue
  3 using System;
  4 using System.Collections.Generic;
  5 using System.ComponentModel;
  6 using System.Data;
  7 using System.Drawing;
  8 using System.Text;
  9 using System.Windows.Forms;
 10 using System.Threading;
 11 
 12 namespace WindowsFormsApplication1
 13 {
 14     public partial class Form1 : Form
 15     {
 16         //A collection of media files than in the playlist
 17         public List<PlayerFile> m_AllFiles = new List<PlayerFile>();
 18         public int P_index=0;
 19         public MediaPlayer.MediaPlayerClass Player;       
 20         public Form1()
 21         {
 22             InitializeComponent();
 23             Player = new MediaPlayer.MediaPlayerClass();           
 24             Player.PlayCount = 1;     
 25         }
 26     
 27         /// <summary>
 28         /// Add Button Click
 29         /// </summary>
 30         /// <param name="sender"></param>
 31         /// <param name="e"></param>
 32         private void button1_Click(object sender, EventArgs e)
 33         {
 34             OpenFileDialog ofd = new OpenFileDialog();
 35             ofd.Filter = "all mp3(*.mp3)|*.mp3|all wmv(*.wmv)|*.wmv|all wma(*.wma)|*.wma";
 36             if (ofd.ShowDialog() != DialogResult.Cancel)
 37             {
 38                 string m_newFilePath = ofd.FileName;
 39                 string m_newFileName=ofd.FileName.Substring(ofd.FileName.LastIndexOf(@"\")+1);
 40                 m_AllFiles.Add(new PlayerFile()
 41                 {
 42                     F_Name =m_newFileName ,
 43                     F_Path =m_newFilePath 
 44                 });
 45             }
 46             listBox1.DataSource = null;
 47             listBox1.Items.Clear();
 48             listBox1.DataSource = m_AllFiles;            
 49         }
 50         /// <summary>
 51         /// Play Button Click
 52         /// </summary>
 53         /// <param name="sender"></param>
 54         /// <param name="e"></param>
 55         private void button3_Click(object sender, EventArgs e)
 56         {
 57             PlayerFile m_PF = listBox1.SelectedItem as PlayerFile;
 58             if (m_PF != null)
 59             {
 60                 Player.Open(m_PF.F_Path);
 61                 Player.Play();                
 62             }
 63         }
 64         /// <summary>
 65         /// Next Button Click
 66         /// </summary>
 67         /// <param name="sender"></param>
 68         /// <param name="e"></param>
 69         private void button4_Click(object sender, EventArgs e)
 70         {
 71             P_index = listBox1.SelectedIndex;
 72             if (P_index < m_AllFiles.Count - 1 && P_index != -1)
 73             {
 74                 P_index++;
 75                 listBox1.SelectedItem = m_AllFiles[P_index];
 76                 button3_Click(sender, e);
 77             }
 78         }
 79         /// <summary>
 80         /// Previous Button Click
 81         /// </summary>
 82         /// <param name="sender"></param>
 83         /// <param name="e"></param>
 84         private void button5_Click(object sender, EventArgs e)
 85         {
 86             P_index = listBox1.SelectedIndex;
 87             if (P_index > 0 && P_index != -1)
 88             {
 89                 P_index--;
 90                 listBox1.SelectedItem = m_AllFiles[P_index];
 91                 button3_Click(sender, e);
 92             }
 93         }
 94         /// <summary>
 95         /// Clear Button Click
 96         /// </summary>
 97         /// <param name="sender"></param>
 98         /// <param name="e"></param>
 99         private void button2_Click(object sender, EventArgs e)
100         {
101             P_index = listBox1.SelectedIndex;
102             if (P_index != -1)
103             {
104                 m_AllFiles.RemoveAt(P_index);
105                 listBox1.DataSource = null;
106                 listBox1.Items.Clear();
107                 listBox1.DataSource = m_AllFiles;
108             }
109             if (listBox1.Items.Count > P_index )
110             {
111                 listBox1.SelectedIndex = P_index;
112             }
113         }
114         /// <summary>
115         /// Double click the items in listbox1
116         /// </summary>
117         /// <param name="sender"></param>
118         /// <param name="e"></param>
119         private void listBox1_DoubleClick(object sender, EventArgs e)
120         {
121             if (listBox1.SelectedIndex != -1)
122             {
123                 button3_Click(sender, e);
124             }
125         }
126 
127         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
128         {
129             Player = null;
130         }
131     }
132     /// <summary>
133     /// Self-defined PlayFile object
134     /// </summary>
135     public class PlayerFile
136     {
137         public string F_Name { getset; }
138         public string F_Path { getset; }
139 
140         public PlayerFile()
141         {
142         }
143 
144         public override string ToString()
145         {
146             return F_Name;
147         }
148     }
149 }
150 

 

 

posted on 2009-12-23 17:20  零点零一  阅读(464)  评论(0)    收藏  举报