用代码打开wav声音文件 做个预定系统软件,要在没有人预定的时候打开wav声音文件,请问怎么样用代码实现打开声音文件。还有怎么判断该声音文件是否播放完毕! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好象在2005中有个控件,你在msdn中查查sound 利用Window Media Player 控件自己做一款小巧的mp3播放器来听音乐 ,是不是很享受呢?今天刚写出来的,听听mp3感觉还不错哦。 闲话少说,进入正题。 Mp3播放器主要完成下列功能: 1. 添加歌曲,可以添加单个乐曲或者指定文件夹内包括其子文件夹内的所有mp3乐曲到播放列表。 2. 删除指定歌曲或所有歌曲。 3. 播放的控制。包括选择上一首,下一首播放,顺序播放,循环播放和随机播放。循环播放又分单个歌曲的循环播放和所有歌曲的循环播放。 首先建立类player。 public class Player { private AxWMPLib.AxWindowsMediaPlayer myPlayer; private string[] playList; private int numOfMusic; private int currentPlay; public int NumOfMusic { get { return numOfMusic; } } public WMPLib.WMPPlayState playstate { get { return myPlayer.playState; } } public string PlayList(int num) { return playList[num]; } public Player(AxWMPLib.AxWindowsMediaPlayer mediaPlayer) { myPlayer = mediaPlayer; playList = new string[1000]; numOfMusic = 0; } public void AddFile(string path) { if(numOfMusic < 1000) { numOfMusic ++; playList[numOfMusic] = path; } } public void DelFile(int selectNum) { for(int i = selectNum; i <= numOfMusic - 1; i++) { playList[i] = playList[i + 1]; } numOfMusic --; } public void play(int selectNum) { myPlayer.URL = playList[selectNum]; currentPlay = selectNum; } public int NextPlay(int type) { /* type = 0 顺序 type = 1 重复播放全部 type = 2 重复播放一首 type = 3 随机播放 */ switch (type) { case 0: currentPlay ++; if(currentPlay > numOfMusic)return 0; else return currentPlay; case 1: currentPlay ++; if(currentPlay > numOfMusic) return 1; else return currentPlay; case 2: return currentPlay; case 3: Random rdm = new Random(unchecked((int)DateTime.Now.Ticks)); currentPlay = rdm.Next() % numOfMusic; if(currentPlay == 0) return numOfMusic; else return currentPlay; default: return 0; } } } Player类中包括一个windowsMediaPlayer对象myPlayer,一个存储播放列表的数组playlist,记录歌曲总数的numOfMusic,以及当前播放的歌曲对应列表中的序号currentplay; 另外有四个方法分别是Play,AddFile,DelFile,以及获得下次播放序号的NextPlay 分功能列出其他主要代码 添加单个歌曲 if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { string path = this.openFileDialog1.FileName; FileInfo f = new FileInfo(path); MyPlayer.AddFile(f.FullName); string STRFILE = Convert.ToString(MyPlayer.NumOfMusic); for(int i = 1;i<=5-STRFILE.Length;i++)STRFILE+=’ ’; STRFILE += f.Name; this.listBox1.Items.Add(STRFILE); } 添加一个文件夹及其所有子文件夹的歌曲 利用递归函数showfiles实现所有层歌曲都添加到歌曲列表中。 private void showfiles(string path,ListBox listBox1) { DirectoryInfo dir = new DirectoryInfo(path); foreach(FileInfo f in dir.GetFiles("*.mp3")) { MyPlayer.AddFile(f.FullName); } foreach(DirectoryInfo f in dir.GetDirectories()) { showfiles(f.FullName,listBox1); } 删除和清空直接调用类Player中的AddFile和DelFile函数 实现播放上一首 if(listBox1.SelectedIndex >= 0) { listBox1.SelectedIndex --; if(listBox1.SelectedIndex <0)listBox1.SelectedIndex = MyPlayer.NumOfMusic - 1; MyPlayer.play(listBox1.SelectedIndex + 1); } 下一首 if(listBox1.SelectedIndex >= 0) { listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % MyPlayer.NumOfMusic; MyPlayer.play(listBox1.SelectedIndex + 1); } 播放的控制 利用Player的NextPlay方法返回的值来选择下一次播放的内容。 同时利用PlayStateChange事件来实现由一曲到下一曲的替换,但是在响应PlayStateChange事件的时候直接改变Player的url无法让它直接播放下一曲,解决方法如下: private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) { if(MyPlayer.playstate == WMPLib.WMPPlayState.wmppsMediaEnded) { timer1.Start(); } } private void timer1_Tick(object sender, System.EventArgs e) { timer1.Stop(); int selectnum = 0; if(menuItem13.Checked)selectnum = MyPlayer.NextPlay(0); else if (menuItem15.Checked)selectnum = MyPlayer.NextPlay(1); else if (menuItem16.Checked)selectnum = MyPlayer.NextPlay(2); else if (menuItem17.Checked)selectnum = MyPlayer.NextPlay(3); if(selectnum != 0) { listBox1.SelectedIndex = selectnum - 1; MyPlayer.play(selectnum); } } 满足一首歌曲结束的条件的时候唤醒计时器,计时器100ms内就响应函数timer1_Tick,在这个函数里实现下一首歌曲的选择播放便可以顺利进行. AxWMPLib.AxWindowsMediaPlayer名称空间是什么啊 楼上的 我用的也是VS2003点击工具箱右键进入添加删除项 COM组件然后选择WindowsMediaPlayer 有的版本不同可能就叫MediaPlayer然后把控件拖到你的Form中如果是AxWindowsMediaPlayer1 则打开文件及播放的方法是:AxWindowsMediaPlayer1.Url="yourmp3.mp3";AxWindowsMediaPlayer1.ctlContrls.Play();(不知道上面ctlContrls拼写正确否)如果是MediaPlayer1 方法是;MediaPlayer1.FileName="yourmp3.mp3";MediaPlayer1.play(); 建议最好不要用Window Media Player 控件来做,兼容性能比较差,最好用API来做在 某 些 情 况 下, 例 如 程 序 员 正 在 编 写 一 个 游 戏, 或 是 将 要 进 行 一 项 非 常 冗 长 无 聊 的 过 程, 可 以 通 过 播 放 音 乐 来 增 加 游 戏 的 声 光 效 果 或 是 减 少 漫 长 的 等 待 所 带 来 的 困 倦。本 文 介 绍 如 何 在Microsoft Visual Basic 中 对 声 音 文 件 进 行 播 放。 播 放 声 音 文 件 ---- 在Microsoft Visual Basic 中 进 行 多 媒 体 音 乐 的 播 放 是 一 件 非 常 轻 松 的 事 情。 在Microsoft Windows 系 统 中 支 持 的 声 音 文 件 有WAV 文 件 和MIDI 文 件, 通 常 使 用Windows 应 用 程 序 编 程 接 口(API) 函 数mciExecute 函 数 可 以 对MIDI 文 件 进 行 播 放。 而 使 用API 函 数sndPlaySound 则 可 以 对WAV 播 放 文 件 进 行 播 放, 当 然, 你 也 可 以 使 用mciExecute 函 数 来 播 放WAV 文 件。 ---- 为 了 播 放WAV 文 件, 你 需 要 在 你 的 程 序 窗 体 的 通 用 声 明 部 分 中, 或 是 标 准 模 块 中 添 加 如 下 的 声 明 语 句: ---- Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long ---- 以 及 如 下 的 标 识 常 量: ---- Public Const SND_SYNC = &H0 ---- Public Const SND_ASYNC = &H1 ---- Public Const SND_NODEFAULT = &H2 ---- Public Const SND_MEMORY = &H4 ---- Public Const SND_ALIAS = &H10000 ---- Public Const SND_FILENAME = &H20000 ---- Public Const SND_RESOURCE = &H40004 ---- Public Const SND_ALIAS_ID = &H110000 ---- Public Const SND_ALIAS_START = 0 ---- Public Const SND_LOOP = &H8 ---- Public Const SND_NOSTOP = &H10 ---- Public Const SND_VALID = &H1F ---- Public Const SND_NOWAIT = &H2000 ---- Public Const SND_VALIDFLAGS = &H17201F ---- Public Const SND_RESERVED = &HFF000000 ---- Public Const SND_TYPE_MASK = &H170007 ---- sndPlaySound 函 数 需 要 两 个 参 数, 第 一 个 参 数soundfilename 是 要 播 放 的WAV 文 件 的 名 称。 第 二 个 参 数 是 一 个 表 明 播 放 方 式 的 标 识 常 量, 其 定 义 的 值 如 上 所 示, 通 常 所 使 用 的 标 识 意 义 如 下: ---- SND_SYNC 播 放WAV 文 件, 播 放 完 毕 后 将 控 制 转 移 回 你 的 应 用 程 序 中。 ---- SND_ASYNC 播 放WAV 文 件, 然 后 将 控 制 立 即 转 移 回 你 的 应 用 程 序 中, 而 不 管 对WAV 文 件 的 播 放 是 否 结 束。 ---- SND_NODEFAULT 不 要 播 放 缺 省 的WAV 文 件, 以 免 发 生 某 些 意 外 的 错 误。 ---- SND_MEMORY 播 放 以 前 已 经 加 载 到 内 存 中 的WAV 文 件。 ---- SND_LOOP 循 环 播 放WAV 文 件。 ---- SND_NOSTOP 在 开 始 播 放 其 它 的WAV 文 件 之 前, 需 要 完 成 对 本WAV 文 件 的 播 放。 ---- 注 意:SND_LOOP 标 识 通 常 需 要 同SND_ASYNC 共 同 使 用, 也 即 在 两 个 标 识 之 间 添 加 与 播 放 符, 以 免 在 对WAV 文 件 进 行 播 放 的 时 候 将 系 统 挂 起。 ---- 为 了 播 放MIDI 文 件, 你 需 要 在 你 的 程 序 窗 体 的 通 用 声 明 部 分 中, 或 是 标 准 模 块 中 添 加 如 下 的 声 明 语 句: ---- Private Declare Function mciExecute Lib "winmm.dll" Alias "mciExecute" (ByVal lpstrCommand As String) As Long ---- mciExecute 函 数 只 需 要 一 个 参 数,CommandString, 顾 名 思 义,它 是 一 个 命 令 字 符 串, 用 于 表 明 对 声 音 文 件 播 放 的 命 令,例 如, 希 望 完 整 播 放 声 音 文 件, 则 该 字 符 串 就 是 字 符 串 这个只能在VS 2005中用。using System.Media;SoundPlayer sp;sp = new SoundPlayer(Application.StartupPath + @"\0037.wav"); sp.PlayLooping(); 求一条正则表达式 为什么 没按IIS ASP.NET能运行 学C#的问题 水晶报表如何实现这样的效果? 自动向客户机子向注册表里注册外接程序 怎么解决TreeView 获取节点的状态 C#如何获取本机的ip地址? 有些类前加[ —里面写些特性—]即类的特性(讨论) 用delphi给C#的webservice发一段中文字符串,服务器收到一堆乱码,如何解决?? 怎样将DataSet中的记录中数据分多次WriteXml? 一个线程池最多可以容纳多小个线程呢? 急!在线等,多线程读取数据并写入的问题
2. 删除指定歌曲或所有歌曲。 3. 播放的控制。包括选择上一首,下一首播放,顺序播放,循环播放和随机播放。循环播放又分单个歌曲的循环播放和所有歌曲的循环播放。 首先建立类player。 public class Player
{
private AxWMPLib.AxWindowsMediaPlayer myPlayer;
private string[] playList;
private int numOfMusic;
private int currentPlay; public int NumOfMusic
{
get
{
return numOfMusic;
}
} public WMPLib.WMPPlayState playstate
{
get
{
return myPlayer.playState;
}
} public string PlayList(int num)
{
return playList[num];
} public Player(AxWMPLib.AxWindowsMediaPlayer mediaPlayer)
{
myPlayer = mediaPlayer;
playList = new string[1000];
numOfMusic = 0;
} public void AddFile(string path)
{
if(numOfMusic < 1000)
{
numOfMusic ++;
playList[numOfMusic] = path;
}
} public void DelFile(int selectNum)
{
for(int i = selectNum; i <= numOfMusic - 1; i++)
{
playList[i] = playList[i + 1];
}
numOfMusic --;
} public void play(int selectNum)
{
myPlayer.URL = playList[selectNum];
currentPlay = selectNum;
} public int NextPlay(int type)
{
/* type = 0 顺序 type = 1 重复播放全部
type = 2 重复播放一首
type = 3 随机播放 */ switch (type)
{
case 0:
currentPlay ++;
if(currentPlay > numOfMusic)return 0;
else return currentPlay;
case 1:
currentPlay ++;
if(currentPlay > numOfMusic) return 1;
else return currentPlay;
case 2:
return currentPlay;
case 3:
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
currentPlay = rdm.Next() % numOfMusic;
if(currentPlay == 0) return numOfMusic;
else return currentPlay;
default:
return 0;
}
}
}
Player类中包括一个windowsMediaPlayer对象myPlayer,一个存储播放列表的数组playlist,记录歌曲总数的numOfMusic,以及当前播放的歌曲对应列表中的序号currentplay; 另外有四个方法分别是Play,AddFile,DelFile,以及获得下次播放序号的NextPlay 分功能列出其他主要代码 添加单个歌曲 if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;
FileInfo f = new FileInfo(path);
MyPlayer.AddFile(f.FullName);
string STRFILE = Convert.ToString(MyPlayer.NumOfMusic);
for(int i = 1;i<=5-STRFILE.Length;i++)STRFILE+=’ ’;
STRFILE += f.Name;
this.listBox1.Items.Add(STRFILE);
}
添加一个文件夹及其所有子文件夹的歌曲 利用递归函数showfiles实现所有层歌曲都添加到歌曲列表中。 private void showfiles(string path,ListBox listBox1)
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dir.GetFiles("*.mp3"))
{
MyPlayer.AddFile(f.FullName);
}
foreach(DirectoryInfo f in dir.GetDirectories())
{
showfiles(f.FullName,listBox1);
}
删除和清空直接调用类Player中的AddFile和DelFile函数 实现播放上一首 if(listBox1.SelectedIndex >= 0)
{
listBox1.SelectedIndex --;
if(listBox1.SelectedIndex <0)listBox1.SelectedIndex = MyPlayer.NumOfMusic - 1;
MyPlayer.play(listBox1.SelectedIndex + 1);
}
下一首 if(listBox1.SelectedIndex >= 0)
{
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % MyPlayer.NumOfMusic;
MyPlayer.play(listBox1.SelectedIndex + 1);
}
播放的控制 利用Player的NextPlay方法返回的值来选择下一次播放的内容。 同时利用PlayStateChange事件来实现由一曲到下一曲的替换,但是在响应PlayStateChange事件的时候直接改变Player的url无法让它直接播放下一曲,解决方法如下: private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if(MyPlayer.playstate == WMPLib.WMPPlayState.wmppsMediaEnded)
{
timer1.Start();
}
} private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
int selectnum = 0;
if(menuItem13.Checked)selectnum = MyPlayer.NextPlay(0);
else if (menuItem15.Checked)selectnum = MyPlayer.NextPlay(1);
else if (menuItem16.Checked)selectnum = MyPlayer.NextPlay(2);
else if (menuItem17.Checked)selectnum = MyPlayer.NextPlay(3);
if(selectnum != 0)
{
listBox1.SelectedIndex = selectnum - 1;
MyPlayer.play(selectnum);
}
}
满足一首歌曲结束的条件的时候唤醒计时器,计时器100ms内就响应函数timer1_Tick,在这个函数里实现下一首歌曲的选择播放便可以顺利进行.
点击工具箱右键进入添加删除项 COM组件
然后选择
WindowsMediaPlayer
有的版本不同可能就叫MediaPlayer
然后把控件拖到你的Form中
如果是AxWindowsMediaPlayer1
则打开文件及播放的方法是:
AxWindowsMediaPlayer1.Url="yourmp3.mp3";
AxWindowsMediaPlayer1.ctlContrls.Play();
(不知道上面ctlContrls拼写正确否)
如果是MediaPlayer1
方法是;
MediaPlayer1.FileName="yourmp3.mp3";
MediaPlayer1.play();
在 某 些 情 况 下, 例 如 程 序 员 正 在 编 写 一 个 游 戏, 或 是 将 要 进 行 一 项 非 常 冗 长 无 聊 的 过 程, 可 以 通 过 播 放 音 乐 来 增 加 游 戏 的 声 光 效 果 或 是 减 少 漫 长 的 等 待 所 带 来 的 困 倦。本 文 介 绍 如 何 在Microsoft Visual Basic 中 对 声 音 文 件 进 行 播 放。 播 放 声 音 文 件 ---- 在Microsoft Visual Basic 中 进 行 多 媒 体 音 乐 的 播 放 是 一 件 非 常 轻 松 的 事 情。 在Microsoft Windows 系 统 中 支 持 的 声 音 文 件 有WAV 文 件 和MIDI 文 件, 通 常 使 用Windows 应 用 程 序 编 程 接 口(API) 函 数mciExecute 函 数 可 以 对MIDI 文 件 进 行 播 放。 而 使 用API 函 数sndPlaySound 则 可 以 对WAV 播 放 文 件 进 行 播 放, 当 然, 你 也 可 以 使 用mciExecute 函 数 来 播 放WAV 文 件。 ---- 为 了 播 放WAV 文 件, 你 需 要 在 你 的 程 序 窗 体 的 通 用 声 明 部 分 中, 或 是 标 准 模 块 中 添 加 如 下 的 声 明 语 句: ---- Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As
Long ---- 以 及 如 下 的 标 识 常 量: ---- Public Const SND_SYNC = &H0
---- Public Const SND_ASYNC = &H1
---- Public Const SND_NODEFAULT = &H2
---- Public Const SND_MEMORY = &H4
---- Public Const SND_ALIAS = &H10000
---- Public Const SND_FILENAME = &H20000
---- Public Const SND_RESOURCE = &H40004
---- Public Const SND_ALIAS_ID = &H110000
---- Public Const SND_ALIAS_START = 0
---- Public Const SND_LOOP = &H8
---- Public Const SND_NOSTOP = &H10
---- Public Const SND_VALID = &H1F
---- Public Const SND_NOWAIT = &H2000
---- Public Const SND_VALIDFLAGS = &H17201F
---- Public Const SND_RESERVED = &HFF000000
---- Public Const SND_TYPE_MASK = &H170007 ---- sndPlaySound 函 数 需 要 两 个 参 数, 第 一 个 参 数soundfilename 是 要 播 放 的WAV 文 件 的 名 称。 第 二 个 参 数 是 一 个 表 明 播 放 方 式 的 标 识 常 量, 其 定 义 的 值 如 上 所 示, 通 常 所 使 用 的 标 识 意 义 如 下: ---- SND_SYNC 播 放WAV 文 件, 播 放 完 毕 后 将 控 制 转 移 回 你 的 应 用 程 序 中。
---- SND_ASYNC 播 放WAV 文 件, 然 后 将 控 制 立 即 转 移 回 你 的 应 用 程 序 中, 而 不 管 对WAV 文 件 的 播 放 是 否 结 束。
---- SND_NODEFAULT 不 要 播 放 缺 省 的WAV 文 件, 以 免 发 生 某 些 意 外 的 错 误。
---- SND_MEMORY 播 放 以 前 已 经 加 载 到 内 存 中 的WAV 文 件。
---- SND_LOOP 循 环 播 放WAV 文 件。
---- SND_NOSTOP 在 开 始 播 放 其 它 的WAV 文 件 之 前, 需 要 完 成 对 本WAV 文 件 的 播 放。 ---- 注 意:SND_LOOP 标 识 通 常 需 要 同SND_ASYNC 共 同 使 用, 也 即 在 两 个 标 识 之 间 添 加 与 播 放 符, 以 免 在 对WAV 文 件 进 行 播 放 的 时 候 将 系 统 挂 起。 ---- 为 了 播 放MIDI 文 件, 你 需 要 在 你 的 程 序 窗 体 的 通 用 声 明 部 分 中, 或 是 标 准 模 块 中 添 加 如 下 的 声 明 语 句: ---- Private Declare Function mciExecute Lib "winmm.dll" Alias "mciExecute" (ByVal lpstrCommand As String) As Long ---- mciExecute 函 数 只 需 要 一 个 参 数,CommandString, 顾 名 思 义,它 是 一 个 命 令 字 符 串, 用 于 表 明 对 声 音 文 件 播 放 的 命 令,例 如, 希 望 完 整 播 放 声 音 文 件, 则 该 字 符 串 就 是 字 符 串
using System.Media;
SoundPlayer sp;
sp = new SoundPlayer(Application.StartupPath + @"\0037.wav");
sp.PlayLooping();