最近有客户问我,怎么把Windows Media Player 嵌套在自己的项目中。
以前我在Delphi下玩过Windows Media Player,可是在Microsoft Visual Studio 2008 没有 测试过。
到网上 搜索了一把,果然得到很多例子。
其中CSDN上有个例子写的不错,不过要扣5分,不过我用自己的分已经下载了,大家要顶啊:
附件: WindowsMediaPlayer1.rar (下载 85 次)
我这个例子就是用这个CSDN上提供的例子,是基于Microsoft Visual Studio 2005下开发的,我只是简单的把这个例子更新到Microsoft Visual Studio 2008 下,不过有点小麻烦,还好,我已经解决了。
不多说了,直接上代码,
Windows Media Player
复制代码
下面是整个工程, 注意:在运行工程之前,需要先添加AxInterop.WMPLib.dll,Interop.MediaPlayer.dll,Interop.WMPLib.dll引用。
以前我在Delphi下玩过Windows Media Player,可是在Microsoft Visual Studio 2008 没有 测试过。
到网上 搜索了一把,果然得到很多例子。
其中CSDN上有个例子写的不错,不过要扣5分,不过我用自己的分已经下载了,大家要顶啊:
附件: WindowsMediaPlayer1.rar (下载 85 次)
我这个例子就是用这个CSDN上提供的例子,是基于Microsoft Visual Studio 2005下开发的,我只是简单的把这个例子更新到Microsoft Visual Studio 2008 下,不过有点小麻烦,还好,我已经解决了。
不多说了,直接上代码,
Windows Media Player
- 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.Threading;
- using AxWMPLib;
- namespace MediaPlayer
- {
-
- public partial class Form1 : Form
- {
- public static Form1 form1;
- public string oldPath;
- //To Load all Songs specified in Default Path
- public DelegateLoadSongs m_loadSongs;
- public delegate void DelegateLoadSongs();
- //Thread used in loading all songs in Default Path.
- public Thread execloadSongs;
- public ToolStripMenuItem parentItem;
- //To Add individual song as a New MenuItem
- public DelegateAddSong m_AddSongs;
- public delegate void DelegateAddSong(string path);
- public Form1()
- {
- InitializeComponent();
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Select Songs.
- bool isFileExist = false;
- WMPLib.IWMPPlaylistCollection playlistColl;
- WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer1.newPlaylist("Test", "");
- if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
- {
- //If multiple Songs are selected,
- //than it will create a New Playlist and add those songs to that list and set it as Current PlayList.
- if (openFileDialog1.FileNames.Length > 1)
- {
- playlistColl = axWindowsMediaPlayer1.playlistCollection;
- playlist = playlistColl.newPlaylist("Test");
- if (recentFilesToolStripMenuItem.DropDownItems.Count == 0)
- {
- foreach (string f in openFileDialog1.FileNames)
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(f, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(f, null, recentFilesMenuItem_Click);
- WMPLib.IWMPMedia media = axWindowsMediaPlayer1.newMedia(f);
- playlist.appendItem(media);
- }
- }
- else
- {
- foreach (string f in openFileDialog1.FileNames)
- {
- isFileExist = false;
- WMPLib.IWMPMedia media = axWindowsMediaPlayer1.newMedia(f);
- playlist.appendItem(media);
- //To add to Recent Play List.
- foreach (ToolStripItem item in recentFilesToolStripMenuItem.DropDownItems)
- {
- if (item.Text == f)
- {
- isFileExist = true;
- break;
- }
- }
- //If selected Song is not Present in Recent Songs List previously,than it will add it.
- if (!isFileExist)
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(f, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(f, null, recentFilesMenuItem_Click);
- }
- }
- }
- axWindowsMediaPlayer1.currentPlaylist = playlist;
- axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- //This Will add selected song to Recent PlayList and Plays it.
- else if (openFileDialog1.FileNames.Length == 1)
- {
- axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
- form1.Text = axWindowsMediaPlayer1.URL;
- if (recentFilesToolStripMenuItem.DropDownItems.Count == 0)
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(openFileDialog1.FileName, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(openFileDialog1.FileName, null, recentFilesMenuItem_Click);
- }
- else
- {
- foreach (ToolStripItem item in recentFilesToolStripMenuItem.DropDownItems)
- {
- if (item.Text == openFileDialog1.FileName)
- {
- isFileExist = true;
- break;
- }
- }
- if (!isFileExist)
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(openFileDialog1.FileName, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(openFileDialog1.FileName, null, recentFilesMenuItem_Click);
- }
- }
- }
- form1.Text = axWindowsMediaPlayer1.URL;
- }
- }
- private void recentFilesMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- //This Will just plays the selected song from Recent PlayList.
- bool isFileExist = false;
- if (File.Exists(((ToolStripItem)sender).Text))
- {
- axWindowsMediaPlayer1.URL = ((ToolStripItem)sender).Text;
- form1.Text = ((ToolStripItem)sender).Text;
- foreach (ToolStripItem item in recentFilesToolStripMenuItem.DropDownItems)
- {
- if (item.Text == ((ToolStripItem)sender).Text)
- {
- isFileExist = true;
- break;
- }
- }
- if (!isFileExist)
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(((ToolStripItem)sender).Text, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(((ToolStripItem)sender).Text, null, recentFilesMenuItem_Click);
- }
- }
- }
- catch { }
- }
- private void playToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Play the selected song.
- axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To pause the selected song.
- axWindowsMediaPlayer1.Ctlcontrols.pause();
- }
- private void stopToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Stop the selected song.
- axWindowsMediaPlayer1.Ctlcontrols.stop();
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Exit Application.
- Application.Exit();
- }
- private void topMostToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Show Media Player on top of other Windows.
- if (topMostToolStripMenuItem.Checked)
- {
- topMostToolStripMenuItem.Checked = false;
- form1.TopMost = false;
- form1.ShowInTaskbar = true;
- }
- else
- {
- topMostToolStripMenuItem.Checked = true;
- form1.TopMost = true;
- form1.ShowInTaskbar = false;
- }
- }
- private void opacityToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Set Opacity of the Form.
- if (opacityToolStripMenuItem.Checked)
- {
- opacityToolStripMenuItem.Checked = false;
- form1.Opacity = 1;
- }
- else
- {
- opacityToolStripMenuItem.Checked = true;
- form1.Opacity = 0;
- }
- }
-
- private void showInTaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Set whether to display Form in TaskBar or not.
- if (showInTaToolStripMenuItem.Checked)
- {
- showInTaToolStripMenuItem.Checked = false;
- form1.ShowInTaskbar = false;
- }
- else
- {
- showInTaToolStripMenuItem.Checked = true;
- form1.ShowInTaskbar = true;
- }
- }
- private void axWindowsMediaPlayer1_MediaError(object sender, AxWMPLib._WMPOCXEvents_MediaErrorEvent e)
- {
- //Generic Error Message.
- MessageBox.Show("Error occured");
- }
- private void showBorderToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Set whether Form Border should be displayed or not.
- if (showBorderToolStripMenuItem.Checked)
- {
- showBorderToolStripMenuItem.Checked = false;
- form1.FormBorderStyle = FormBorderStyle.None;
- }
- else
- {
- showBorderToolStripMenuItem.Checked = true;
- form1.FormBorderStyle = FormBorderStyle.Sizable;
- }
-
- }
- private void notifyIcon1_DoubleClick(object sender, EventArgs e)
- {
- //To Set Visibility of the Form.
- try
- {
- if (form1.Visible)
- {
- form1.Visible = false;
- form1.Opacity = 1;
- }
- else
- {
- form1.Visible = true;
- }
- }
- catch
- { }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- //To Change NotifyIcon Image.
- try
- {
- Random r = new Random();
- notifyIcon1.Icon = new Icon("..\\..\\Images\\" + r.Next(1,11) + ".ico");
- }
- catch { }
- }
- private void menuStrip1_MouseHover(object sender, EventArgs e)
- {
- //T Show Form Border.
- form1.FormBorderStyle = FormBorderStyle.Sizable;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //To Load Recent PlayList Items,Default songs Path from the File.
- if (File.Exists(@"c:\Media PlayList\List.txt"))
- {
- StreamReader reader = new StreamReader(@"c:\Media PlayList\List.txt");
- string fname = "";
- while ((fname = reader.ReadLine()) != null)
- {
- if (!fname.StartsWith("----"))
- {
- recentFilesToolStripMenuItem.DropDownItems.Add(fname, null, recentFilesMenuItem_Click);
- RecentListcontextMenuStrip.Items.Add(fname, null, recentFilesMenuItem_Click);
- }
- else
- {
- //Default Songs Path.
- DefaultPathtoolStripTextBox.Text = fname.TrimStart('-');
- //To Load all songs present in Default Songs Path.
- DefaultPathtoolStripTextBox_KeyDown(sender, null);
- }
- }
- reader.Close();
- }
-
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- //To Save Recent PlayList Items,Default Songs Path to the file.
- if (!Directory.Exists(@"c:\Media PlayList"))
- {
- Directory.CreateDirectory(@"c:\Media PlayList");
- }
- File.Delete(@"c:\Media PlayList\List.txt");
- File.Create(@"c:\Media PlayList\List.txt").Close();
- System.IO.StreamWriter writer = new StreamWriter(@"c:\Media PlayList\List.txt");
- foreach (ToolStripMenuItem fItem in recentFilesToolStripMenuItem.DropDownItems)
- {
- writer.WriteLine(fItem.Text);
- }
- writer.WriteLine("----" + DefaultPathtoolStripTextBox.Text.Trim());
- writer.Close();
- //To Kill the thread used to load Default Path Songs.
- if (execloadSongs != null)
- {
- if (execloadSongs.IsAlive)
- {
- execloadSongs.Abort();
- execloadSongs = null;
- }
- }
- }
- private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Show Form Border.
- form1.FormBorderStyle = FormBorderStyle.Sizable;
- }
- private void DeleteFListToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Delete Recent Songs List.
- recentFilesToolStripMenuItem.DropDownItems.Clear();
- File.Delete(@"c:\Media PlayList\List.txt");
- }
- private void axWindowsMediaPlayer1_MouseUpEvent(object sender, _WMPOCXEvents_MouseUpEvent e)
- {
- //To Set Visibility of Form's Border,MenuStrip.
- if (menuStrip1.Visible)
- {
- if (!showBorderToolStripMenuItem.Checked)
- {
- menuStrip1.Visible = false;
- form1.FormBorderStyle = FormBorderStyle.None;
- axWindowsMediaPlayer1.uiMode = "None";
- axWindowsMediaPlayer1.windowlessVideo = true;
- }
- }
- else
- {
- if (!showBorderToolStripMenuItem.Checked)
- {
- menuStrip1.Visible = true;
- form1.FormBorderStyle = FormBorderStyle.Sizable;
- axWindowsMediaPlayer1.uiMode = "full";
- axWindowsMediaPlayer1.windowlessVideo = false;
- }
- }
- }
- private void LoadAllSongs()
- {
- try
- {
- //To Load all songs present in Default Songs Path.
- string paths = DefaultPathtoolStripTextBox.Text.Trim();
- string[] allpaths = paths.Split(';');
- foreach (string path in allpaths)
- {
- m_AddSongs = new DelegateAddSong(this.AddSong);
- if (Directory.Exists(path))
- {
- DirectoryInfo dirinfo1 = new DirectoryInfo(Path.GetDirectoryName(path));
- Image img = Image.FromFile("..\\..\\Images\\folder.gif");
- parentItem = SongsListtoolStripMenuItem;
- form1.getDirs(dirinfo1);
- if (recursivelyToolStripMenuItem.Checked)
- {
- LoadAllSongs(SongsListtoolStripMenuItem);
- }
- }
- }
- }
- catch { }
- }
- public void getDirs(DirectoryInfo d)
- {
- //To Load All Directories.
- DirectoryInfo[] dirs = d.GetDirectories("*.*");
- getFiles(d);
- foreach (DirectoryInfo dir in dirs)
- {
- this.Invoke(m_AddSongs, new object[] { dir.FullName });
- }
- }
- public void getFiles(DirectoryInfo d)
- {
- //To Load All Files in a Directory.
- FileInfo[] files;
- files = d.GetFiles("*.*");
- foreach (FileInfo file in files)
- {
- String fileName = file.FullName;
- this.Invoke(m_AddSongs, new object[] { file.FullName + "??" });
- }
- }
- private void LoadAllSongs(ToolStripMenuItem item)
- {
- //Recursion used to load all directory's contents.
- foreach (ToolStripMenuItem item1 in item.DropDownItems)
- {
- if (oldPath != item1.Text)
- {
- if (item1.Image != null)
- {
- parentItem = item1;
- oldPath = item1.Text;
- form1.getDirs(new DirectoryInfo(item1.Text));
- LoadAllSongs(item1);
- }
- }
- }
- }
- private void AddSong(string path)
- {
- try
- {
- //To add a song to the Default songs LIst
- if (path.EndsWith("??"))
- {
- //To Add Files without any Image in it.
- parentItem.DropDownItems.Add(path.TrimEnd('?'), null, recentFilesMenuItem_Click);
- }
- else
- {
- ToolStripMenuItem item = new ToolStripMenuItem();
- Image img = Image.FromFile("..\\..\\Images\\folder.gif");
- item.Text = path.TrimEnd('?');
- item.Image = img;
- parentItem.DropDownItems.Add(item);
- }
- }
- catch { }
- }
- private void DefaultPathtoolStripTextBox_KeyDown(object sender, KeyEventArgs e)
- {
- //To Load all Songs,Once Default Songs Path is Changed.
- if (e == null)
- {
- SongsListtoolStripMenuItem.DropDownItems.Clear();
- m_loadSongs = new DelegateLoadSongs(this.LoadAllSongs);
- ThreadStart ts = new ThreadStart(this.m_loadSongs);
- execloadSongs = new Thread(ts);
- execloadSongs.Start();
- return;
- }
- if (e.KeyCode == Keys.Enter)
- {
- SongsListtoolStripMenuItem.DropDownItems.Clear();
- m_loadSongs = new DelegateLoadSongs(this.LoadAllSongs);
- ThreadStart ts = new ThreadStart(this.m_loadSongs);
- execloadSongs = new Thread(ts);
- execloadSongs.Start();
- }
- }
- private void recursivelyToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //To Set whether songs should be loaded recursively in Folders or not.
- if (recursivelyToolStripMenuItem.Checked)
- {
- recursivelyToolStripMenuItem.Checked = false;
- }
- else
- {
- recursivelyToolStripMenuItem.Checked = true;
- }
- }
- private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Middle)
- {
- //To change the Player status on Click on NotifyIcon using Middle Mouse's Button.
- if (axWindowsMediaPlayer1.status != "")
- {
- if (axWindowsMediaPlayer1.status.ToLower().StartsWith("playing"))
- {
- axWindowsMediaPlayer1.Ctlcontrols.pause();
- }
- else if (axWindowsMediaPlayer1.status.ToLower().StartsWith("paused"))
- {
- axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- }
- }
- }
- }
- }