基于Windows Media Player, 写自己的播放器【有源码,可下载整个工程】
来源:互联网 发布:神作 知乎 编辑:程序博客网 时间:2024/04/29 16:40
最近有客户问我,怎么把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();
- }
- }
- }
- }
- }
- }
- WindowsMediaPlayer.rar
- 基于Windows Media Player, 写自己的播放器【有源码,可下载整个工程】
- 【MFC】基于windows media player的音乐播放器
- 一个javascript写的media player播放器
- 一个javascript写的media player播放器
- 一个基于Window Media Player控件的简单播放器
- 基于ActiveX Media Player 的MFC音乐播放器(一)
- 基于ActiveX Media Player 的MFC音乐播放器(二)
- windows Media Player 9.0网页播放器
- Windows Media Player播放器(VC++ 2008)
- Windows Media Player播放器(VC++ 2008)
- Windows Media Player播放器(VC++ 2008)
- js 调用windows media player 播放器
- Winform Windows Media Player 简易播放器
- Windows Media Player 网页播放器
- 各种网页播放器源码 Windows Media Player 网页播放器 及其相关参数含义
- Media Player的播放
- Windows Media Player 无法播放
- 解决windows media player无法播放视频,下载解码器失败的问题!
- Ken Rockwell的"摄影师的七级分类"
- 自动生成Makefile的全过程详解!
- C#实现的全国主要城市公交线路下载器
- 表单文本输入框默认值的自动清除和恢复 -- defaultValue/setAttribute/focus/blur
- C++ new、delete、free关系
- 基于Windows Media Player, 写自己的播放器【有源码,可下载整个工程】
- 旅游景点大全网站
- 什么是“面向对象编程”?用邓小平理论指导编程思想
- C#识别验证码
- 学习的点点滴滴
- C#文章采集浅析
- Tor Browser Bundle(Tor+Firefox)安装flash player插件
- C# 制作的WEB图片批量下载器
- 指针的数组,数组的指针