右击工具箱->选择项 -> 显示"选择工具箱项" -> COM组件 -> Windows Media Player wmp.dll 添加
MyMediaPlayer窗体
public static string path = "";//记录文件完整路径
string duration = "";//当前文件播放的时间
int width = 0;//播放文件的宽度
int height = 0;//播放文件的高度
//存放路径key为文件名,value为完整路径
private Dictionary
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.OpenFile();
//清除播放列表中所有的值
this.listView1.Items.Clear();
//清除集合
pathList.Clear();
//添加到Listview中
listViewUpdate(Path.GetFileName(path), path);
//播放
axWmp.URL = path;
//当打开播放文件,启动timer控件,得到文件的时间,和宽度高度。
//如果放在当前位置,得到的数值为0,
//可能因为媒体文件的打开需要一定时间,这里等待媒体文件的打开
timer1.Start();
}
private void OpenFile() {
//打开一个文件
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Cancel)
{ //取消打开
return;
}
//否则记录打开路径
path = ofd.FileName;
}
private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
{
//只记录文件名
//String s=Path.GetFileNameWithoutExtension(path);
//记录文件名和扩展名
string ss = Path.GetFileName(path);
//播放文件的信息
MusicInfo mi = new MusicInfo(ss,duration,width,height);
mi.ShowDialog();
}
private void 播放ToolStripMenuItem1_Click(object sender, EventArgs e)
{
//播放文件
this.axWmp.Ctlcontrols.play();
}
private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
//暂停播放
this.axWmp.Ctlcontrols.pause();
}
private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.axWmp.Ctlcontrols.stop();
//清除图像,等待就须状态
this.axWmp.close();
}
private void 升高音量ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.axWmp.settings.volume += 5;
}
private void timer1_Tick(object sender, EventArgs e)
{
//得到文件播放总时间和大小
duration = this.axWmp.currentMedia.durationString;
width = axWmp.currentMedia.imageSourceWidth;
height = axWmp.currentMedia.imageSourceHeight;
}
private void 降低音量ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.axWmp.settings.volume -= 5;
}
private void 静音ToolStripMenuItem_Click(object sender, EventArgs e)
{
// this.axWmp.settings.volume = 0;
if (this.axWmp.settings.mute == true) {//true代表静音
this.axWmp.settings.mute =false;
}
else if (this.axWmp.settings.mute == false) {
this.axWmp.settings.mute = true;
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//选择一个文件
this.OpenFile();
//打开路径取出文件名及其扩展名
string musicFileName = Path.GetFileName(path);
listViewUpdate(musicFileName,path);
}
//添加到listview
//文件名和文件完整路径名
private void listViewUpdate(string name,string path) {
//判断是否包含键值
if (pathList.ContainsKey(name)) {//包含返回真
return;
}
pathList.Add(name, path);
ListViewItem lvi = new ListViewItem(name);
listView1.Items.Add(lvi);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
//通过文件名,获得完整路径
this.axWmp.URL =pathList[listView1.SelectedItems[0].Text];
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//删除播放列表项
//1首先删除集合中的值,然后删除Listview
if (listView1.SelectedItems.Count == 0) {
return;
}
pathList.Remove(listView1.SelectedItems[0].Text);
//从listview1中删除
//删除以后还能播放
this.listView1.Items.Remove(listView1.SelectedItems[0]);
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
About a = new About();
a.ShowDialog();
}
MusicInfo窗体
private string Info="";//获取另一个窗口的信息
private string Duration;
private int Width=0;
private int Height=0;
public MusicInfo(string info,string duration,int width,int height) {
Info = info;
Duration = duration;
Width = width;
Height = height;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//判断是否有文件播放
if (String.IsNullOrEmpty(Info))
{
this.lblFileName.Text = "无播放文件";
}
else {
this.lblFileName.Text = Info;
}
//Graphics gh = this.CreateGraphics();
//gh.DrawLine(Pens.Blue, new Point(100, 10), new Point(100, 300));
//判断文件类型
string Extensiona = Path.GetExtension(Info);
if (Extensiona == ".wmv")
{
this.lblFileType.Text = "视频";
}
else {
this.lblFileType.Text = "音频";
}
//获取文件大小
if (string.IsNullOrEmpty(Info))
{
this.lblFileSize.Text = "0MB";
}
else {
FileInfo fi = new FileInfo(Info);
long fileSize = fi.Length / 1024 / 1024;
this.lblFileSize.Text = fileSize + "MB";
}
//当前播放时间
this.lblDuration.Text = Duration;
//分辨率
if (Width == 0 &&Height == 0) {
lblResolution.Text = "无视频";
}
this.lblResolution.Text = Width + "x" + Height;
//详细路径
//第二种方法获得另一个窗体的路径
//当创建新的对象path路径为空,做静态成员
// MyMediaPlayer MP = new MyMediaPlayer();
this.lblFilePositon.Text =MyMediaPlayer.path;
}
1、CD、VCD音频播放
首先需要添加Wiindows Media Player组件,方法:
(1)“工具箱”右键“选择项”。
(2)在弹出的“选择工具箱项”对话框中选择“COM组件”选项卡。
(3)在COM组件列表中选择Windows Media Player,单击确定,ok。
事例代码:
private void 选取文件_Click(object sender, EventArgs e)
{
this.optFile.ShowDialog();
this.axWindowsMediaPlayer1.newMedia(this.optFile.FileName);
}
private void 播放文件_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = this.optFile.FileName;
}
private void 停止_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.close();
}
暂停: this.axWindowsMediaPlayer1.Ctlcontrols.pause();
继续播放:this.axWindowsMediaPlayer1.Ctlcontrols.play();
获取歌曲信息:
private WMPLib.WindowsMediaPlayerClass c;
private WMPLib.IWMPMedia m;
private void ButInfo_Click(object sender, EventArgs e)
{
if (this.optFile.FileName != "optFile")
{
c = new WMPLib.WindowsMediaPlayerClass();
m = c.newMedia(this.optFile.FileName);
MessageBox.Show("歌手名:" + m.getItemInfo("Author") + "/r/n" + "歌 名:" + m.getItemInfo("Title"));
}
}
2、MP3 、WAV播放
1)带记忆功能的MP3 播放器
具有带记忆功能只需要在程序退出时,将当前用户选择的文件保存到ini文件中就可以了。
如:在打开文件的同时写入到ini文件中:
string strpath;
public Form1() //构造函数
{
InitializeComponent();
strpath = System.Environment.CurrentDirectory;
}
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
StreamWriter s = new StreamWriter(strpath + "//HyList.ini", true);
s.WriteLine(openFileDialog1.FileName);
s.Flush();
s.Close();
ShowWindows(openFileDialog1.FileName); //调用函数
}
public void ShowWindows(string fileName)
{
this.listBox1.Items.Add(fileName);
}
在打开窗体的时候加载ini文件中的数据:
private void Form1_Load(object sender, EventArgs e)
{
string str = Application.StartupPath;
StreamReader sr = new StreamReader(str + "//HyList.ini");
while (sr.Peek() >= 0)
{
string strk=sr.ReadLine();
if (strk!="")
listBox1.Items.Add(strk);
}
sr.Close();
}
播放选中文件:
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count > 0)
{
string strPath = listBox1.SelectedItem.ToString();
ShowPlay(strPath);
}
}
2)、自动播放的MP3播放器
(1)自动添加播放列表:
private void button1_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
if (this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
GetAllFiles(dir); //调用函数
}
}
public void GetAllFiles(DirectoryInfo dir)
{
this.listBox1.Items.Clear();
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回表示某个目录中所有文件和子目录的强类型System.IO.FileSystemInfo项的数组
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo)
{
GetAllFiles((DirectoryInfo)i);
}
else
{
string str = i.FullName;
int b = str.LastIndexOf("//");
string strbbb = str.Substring(b + 1);
if (strbbb.Substring(strbbb.Length - 3) == "mp3")
{
this.listBox1.Items.Add(str.Substring(b + 1));
//添加列表
WC = new WMPLib.WindowsMediaPlayerClass();
MC = WC.newMedia(str);
this.axWindowsMediaPlayer1.currentPlaylist.appendItem(MC);
}
}
}
}
播放所有文件:
private void button2_Click(object sender, EventArgs e)
{
if (MC != null)
this.axWindowsMediaPlayer1.Ctlcontrols.play();
else
MessageBox.Show("请添加文件列表");
}
3、播放动画
1)播放Flash动画
首先需要计算机中有Flash插件,添加过程如下:
(1)选择“工具箱”,单击鼠标右键,在弹出的快捷菜单中选择“选择项”。
(2)弹出“选择工具箱项”对话框,选择“COM组件”选择卡。
(3)在COM组件列表中,单击[浏览]按钮,在对话框中选择“//SYSTEM32/Macromed/Flash/SWFLASH.OCX”.
打开文件事件:
try
{
openFileDialog1.Filter = "Flash文件(.swf)|.swf|所有文件(.)|.";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string MyFileName = openFileDialog1.FileName;
this.axShockwaveFlash1.Movie = MyFileName;
}
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
//播放
private void button6_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Rewind();
this.axShockwaveFlash1.Play();
}
//第一帧
private void button3_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Rewind();
}
//上一帧
private void button4_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Back();
}
//下一帧
private void button5_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Forward();
}
//停止
private void button2_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Stop();
}
2)播放制作AVI播放器
需添加“COM组件”中“Microsoft Animation Control,Version 6.0”
主要方法: open()方法,eg. this.axAnimation1.Open(Application.StartupPath + "//clock.avi");
stop()方法,
Play()方法,播放文件。参数this.axAnimation1.Play(播放次数, 播放开始帧, 播放结束帧);
4、播放Gif动画
Bitmap bitmap = new Bitmap(Application.StartupPath+"//1.gif");
bool current = false;
播放事件:
private void button1_Click(object sender, EventArgs e)
{
PlayImage();
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));//播放
}
public void PlayImage()
{
if (!current)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
current = true;
}
}
停止播放:
private void button2_Click(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));//停止
}
转自:https://blog.csdn.net/suxuelian/article/details/49760475