.net用C#编写一个media播放器
来源:互联网 发布:广电网络股票吧 编辑:程序博客网 时间:2024/05/16 07:45
这个是仿照《Visual C# 2005 入门与提高》中的代码写的.
其实还支持wma等wmplayer支持的格式.(因为解码器是调用的mediaplayer控件).
界面:
代码如下;
//http://blog.csdn.net/greenerycn
//请遵守署名非商业的CC版权.
//greenerycn@163.com
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.Runtime.InteropServices;
namespace MyMediaPlayer
......{
public partial class Form1 : Form
......{
[DllImport ("winmm.dll")]
public static extern int waveOutGetNumDevs(string lpszName,int hModule,int dwFlags); //need for the WinAPI
/**//**//**//// <summary>
/// 歌曲列表
/// </summary>
private string[] playlist = new string[500];
/**//**//**//// <summary>
/// 歌曲数
/// </summary>
private int numofMusic;
/**//**//**//// <summary>
/// 添加文件
/// </summary>
/// <param name="path">文件路径</param>
public void AddFile(string path)
......{
if (numofMusic < 500)
......{
numofMusic++;
playlist[numofMusic] = path;
}
}
/**//**//**//// <summary>
/// 删除文件
/// </summary>
/// <param name="selectNum">删除文件名</param>
public void DelFile(int selectNum)
......{
for (int i = selectNum; i <= numofMusic - 1; i++)
playlist[i] = playlist[i + 1];
numofMusic--;
}
public void PlaySong(int selectNum)
......{
MediaPlayer1.URL = playlist[selectNum];
}
public void AddDir(string path, ListBox listBox1)
......{
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo f in dir.GetFiles("*.mp3"))
......{
AddFile(f.FullName);
string strTmp = Convert.ToString(numofMusic);
for (int i = 1; i < (5 - strTmp.Length); i++)
strTmp += ' ';
strTmp += "- " + f.Name;
this.listBox1.Items.Add(strTmp);
}
foreach (DirectoryInfo f in dir.GetDirectories())
......{
AddDir(f.FullName, listBox1);
}
}
public Form1()
......{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
......{
try
......{
linkLabel1.LinkVisited = true;
System.Diagnostics.Process.Start("http://blog.csdn.net/greenerycn");
}
catch
......{
MessageBox.Show("Unable to open link that was clicked.");
}
}
private void Form1_Load(object sender, EventArgs e)
......{
listBox1.Items.CopyTo(playlist,0);
numofMusic=0;
}
/**//**//**//// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
......{
int selectOne;
if (listBox1.SelectedIndex < 0)
......{
selectOne = 1;
listBox1.SelectedIndex = 0;
}
else
selectOne = listBox1.SelectedIndex + 1;
if (listBox1.Items.Count<0)
listBox1.SelectedIndex=0;
PlaySong(selectOne);
}
private void button2_Click(object sender, EventArgs e)
......{
if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
......{
string path=this.openFileDialog1.FileName;
FileInfo f=new FileInfo(path);
AddFile(f.FullName);
string strTmp=Convert.ToString(numofMusic);
for (int i=1;i<(5-strTmp.Length);i++)
strTmp+= ' ';
strTmp+="- "+f.Name;
this.listBox1.Items.Add(strTmp);
}
}
private void button3_Click(object sender, EventArgs e)
......{
folderBrowserDialog1.SelectedPath="C:";
folderBrowserDialog1.ShowNewFolderButton=true;
folderBrowserDialog1.Description="请选择音乐文件目录";
folderBrowserDialog1.ShowDialog();
AddDir(folderBrowserDialog1.SelectedPath,listBox1);
}
private void button4_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex>=0)
......{
DelFile(listBox1.SelectedIndex+1);
listBox1.Items.Remove(listBox1.SelectedIndex);
}
}
private void button5_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex>=0)
......{
listBox1.SelectedIndex--;
if (listBox1.SelectedIndex<0)
listBox1.SelectedIndex=numofMusic-1;
PlaySong(listBox1.SelectedIndex+1);
}
}
/**//**//**//// <summary>
/// 下一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex<listBox1.Items.Count-1)
listBox1.SelectedIndex++;
else if (listBox1.Items.Count>0)
listBox1.SelectedIndex=0;
PlaySong(listBox1.SelectedIndex+1);
}
private void button7_Click(object sender, EventArgs e)
......{
MediaPlayer1.URL="";
}
private void listBox1_DoubleClick(object sender, EventArgs e)
......{
int selectOne;
if (listBox1.SelectedIndex<0)
selectOne=1;
else
selectOne=listBox1.SelectedIndex+1;
if (listBox1.Items.Count<0)
listBox1.SelectedIndex=0;
PlaySong(selectOne);
}
//用winAPI检测声卡
private void CheckSoundCard_Click(object sender, EventArgs e)
......{
int i;
i = waveOutGetNumDevs("", 0, 0);
if (i > 0)
MessageBox.Show("可以播放声音。", "声音检测", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("不可以播放声音。", "声音检测", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
//请遵守署名非商业的CC版权.
//greenerycn@163.com
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.Runtime.InteropServices;
namespace MyMediaPlayer
......{
public partial class Form1 : Form
......{
[DllImport ("winmm.dll")]
public static extern int waveOutGetNumDevs(string lpszName,int hModule,int dwFlags); //need for the WinAPI
/**//**//**//// <summary>
/// 歌曲列表
/// </summary>
private string[] playlist = new string[500];
/**//**//**//// <summary>
/// 歌曲数
/// </summary>
private int numofMusic;
/**//**//**//// <summary>
/// 添加文件
/// </summary>
/// <param name="path">文件路径</param>
public void AddFile(string path)
......{
if (numofMusic < 500)
......{
numofMusic++;
playlist[numofMusic] = path;
}
}
/**//**//**//// <summary>
/// 删除文件
/// </summary>
/// <param name="selectNum">删除文件名</param>
public void DelFile(int selectNum)
......{
for (int i = selectNum; i <= numofMusic - 1; i++)
playlist[i] = playlist[i + 1];
numofMusic--;
}
public void PlaySong(int selectNum)
......{
MediaPlayer1.URL = playlist[selectNum];
}
public void AddDir(string path, ListBox listBox1)
......{
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo f in dir.GetFiles("*.mp3"))
......{
AddFile(f.FullName);
string strTmp = Convert.ToString(numofMusic);
for (int i = 1; i < (5 - strTmp.Length); i++)
strTmp += ' ';
strTmp += "- " + f.Name;
this.listBox1.Items.Add(strTmp);
}
foreach (DirectoryInfo f in dir.GetDirectories())
......{
AddDir(f.FullName, listBox1);
}
}
public Form1()
......{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
......{
try
......{
linkLabel1.LinkVisited = true;
System.Diagnostics.Process.Start("http://blog.csdn.net/greenerycn");
}
catch
......{
MessageBox.Show("Unable to open link that was clicked.");
}
}
private void Form1_Load(object sender, EventArgs e)
......{
listBox1.Items.CopyTo(playlist,0);
numofMusic=0;
}
/**//**//**//// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
......{
int selectOne;
if (listBox1.SelectedIndex < 0)
......{
selectOne = 1;
listBox1.SelectedIndex = 0;
}
else
selectOne = listBox1.SelectedIndex + 1;
if (listBox1.Items.Count<0)
listBox1.SelectedIndex=0;
PlaySong(selectOne);
}
private void button2_Click(object sender, EventArgs e)
......{
if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
......{
string path=this.openFileDialog1.FileName;
FileInfo f=new FileInfo(path);
AddFile(f.FullName);
string strTmp=Convert.ToString(numofMusic);
for (int i=1;i<(5-strTmp.Length);i++)
strTmp+= ' ';
strTmp+="- "+f.Name;
this.listBox1.Items.Add(strTmp);
}
}
private void button3_Click(object sender, EventArgs e)
......{
folderBrowserDialog1.SelectedPath="C:";
folderBrowserDialog1.ShowNewFolderButton=true;
folderBrowserDialog1.Description="请选择音乐文件目录";
folderBrowserDialog1.ShowDialog();
AddDir(folderBrowserDialog1.SelectedPath,listBox1);
}
private void button4_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex>=0)
......{
DelFile(listBox1.SelectedIndex+1);
listBox1.Items.Remove(listBox1.SelectedIndex);
}
}
private void button5_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex>=0)
......{
listBox1.SelectedIndex--;
if (listBox1.SelectedIndex<0)
listBox1.SelectedIndex=numofMusic-1;
PlaySong(listBox1.SelectedIndex+1);
}
}
/**//**//**//// <summary>
/// 下一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
......{
if (listBox1.SelectedIndex<listBox1.Items.Count-1)
listBox1.SelectedIndex++;
else if (listBox1.Items.Count>0)
listBox1.SelectedIndex=0;
PlaySong(listBox1.SelectedIndex+1);
}
private void button7_Click(object sender, EventArgs e)
......{
MediaPlayer1.URL="";
}
private void listBox1_DoubleClick(object sender, EventArgs e)
......{
int selectOne;
if (listBox1.SelectedIndex<0)
selectOne=1;
else
selectOne=listBox1.SelectedIndex+1;
if (listBox1.Items.Count<0)
listBox1.SelectedIndex=0;
PlaySong(selectOne);
}
//用winAPI检测声卡
private void CheckSoundCard_Click(object sender, EventArgs e)
......{
int i;
i = waveOutGetNumDevs("", 0, 0);
if (i > 0)
MessageBox.Show("可以播放声音。", "声音检测", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("不可以播放声音。", "声音检测", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
- .net用C#编写一个media播放器
- C# Media播放器
- C#调用Windows Media Player制作播放器
- media 播放器
- android media 播放器
- android media 播放器
- C#Media播放器
- Android下用Media Player实现一个最简单的视频播放器
- 一个javascript写的media player播放器
- 一个基于Window Media Player控件的简单播放器
- 一个javascript写的media player播放器
- C#编写简单音乐播放器
- 用C#和vs2015编写了一个简单音乐播放器,实现功能,多条音乐导入,播放、暂停、上下曲。采用vs2105自带的一个播放器控件
- C#使用Windows Media Player播放音频文件
- PHP编写一个mp3音乐播放器,基于DOS的播放器你用过吗?
- Window Media Player 播放器
- media player播放器参数设置
- 视频播放器 media player
- CIO走向成功的12步计划
- 孙鑫vc学习笔记_第11课_part3
- 孙鑫vc学习笔记_第11课_part4
- 学习于丹《论语》心得
- Div+CSS布局入门教程
- .net用C#编写一个media播放器
- 12月25日——培训第31天
- Delphi中ShellExecute的妙用
- HTMLENCODE和URLENCODE的区别
- 设置tomcat5.5以上的jvm最大内存值
- 龙芯软件开发(13)--配置南桥进入调试新天地
- 大型网站新闻系统如何生成静态网页
- 12月26日——培训第32天
- asp.net 2.0 将文字转换成图片