C#中声音的播放主要有六种方法:
1.播放系统事件声音
2.使用SoundPlayer
3.使用API函数播放
4.使用axWindowsMediaPlayer的COM组件来播放
5.Microsoft speech object Library
6.使用directX
1.
播放系统事件声音
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"D:\test.wav";
player.Load(); //同步加载声音
player.Play(); //启用新线程播放
//player.PlayLooping(); //循环播放模式
//player.PlaySync(); //UI线程同步播放
[DllImport(
"
winmm.dll
"
, SetLastError =
true
)]
static
extern
bool
PlaySound(
string
pszSound, UIntPtr hmod,
uint
fdwSound);
[DllImport(
"
winmm.dll
"
, SetLastError =
true
)]
static
extern
long
mciSendString(
string
strCommand,
StringBuilder strReturn,
int
iReturnLength, IntPtr hwndCallback);
[DllImport(
"
winmm.dll
"
)]
private
static
extern
long
sndPlaySound(
string
lpszSoundName,
long
uFlags);
[Flags]
public
enum
SoundFlags
///
<summary>
play synchronously (default)
</summary>
SND_SYNC =
0x0000
,
///
<summary>
play asynchronously
</summary>
SND_ASYNC =
0x0001
,
///
<summary>
silence (!default) if sound not found
</summary>
SND_NODEFAULT =
0x0002
,
///
<summary>
pszSound points to a memory file
</summary>
SND_MEMORY =
0x0004
,
///
<summary>
loop the sound until next sndPlaySound
</summary>
SND_LOOP =
0x0008
,
///
<summary>
don’t stop any currently playing sound
</summary>
SND_NOSTOP =
0x0010
,
///
<summary>
Stop Playing Wave
</summary>
SND_PURGE =
0x40
,
///
<summary>
don’t wait if the driver is busy
</summary>
SND_NOWAIT =
0x00002000
,
///
<summary>
name is a registry alias
</summary>
SND_ALIAS =
0x00010000
,
///
<summary>
alias is a predefined id
</summary>
SND_ALIAS_ID =
0x00110000
,
///
<summary>
name is file name
</summary>
SND_FILENAME =
0x00020000
,
///
<summary>
name is resource name or atom
</summary>
SND_RESOURCE =
0x00040004
public
static
void
Play(
string
strFileName)
PlaySound(strFileName, UIntPtr.Zero,
(
uint
)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC |
SoundFlags.SND_NOSTOP));
public
static
void
mciPlay(
string
strFileName)
string
playCommand =
"
open
"
+ strFileName +
"
type WAVEAudio alias MyWav
"
;
mciSendString(playCommand,
null
,
0
, IntPtr.Zero);
mciSendString(
"
play MyWav
"
,
null
,
0
, IntPtr.Zero);
public
static
void
sndPlay(
string
strFileName)
sndPlaySound(strFileName, (
long
)SoundFlags.SND_SYNC);
关于mciSendString的详细参数说明,请参见MSDN,或是http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
4.使用axWindowsMediaPlayer的COM组件来播放
选择菜单中的“工具”中的“自定义工具箱(添加/移除工具箱项)”,在自定义工具箱的窗口中,点击展开“COM 组件”项,选中“WindowMedia Player”选项。确定后在“工具箱”中便会出现“Windows Media Player”这一项,然后再将其拖至Form上,调整大小,系统在“引用”中自动加入了对此dll的引用,AxMediaPlayer就是我们使用的 Namespace与class。
把Windows Media Player控件拖放到Winform窗体中,把axWindowsMediaPlayer1中URL属性设置为MP3或是AVI的文件路径。
private voidmenuItem1_Click(object sender, System.EventArgs e)
OpenFileDialogofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;
//the nextsentence must be in single line
ofDialog.Filter = "VCD文件(*.dat) | *.dat | Audio文件(*.avi) | *.avi | WAV文件(*.wav) | *.wav | MP3文件(*.mp3) | *.mp3 | 所有文件(*.*) | *.* ";
ofDialog.DefaultExt = "*.mp3";
if (ofDialog.ShowDialog() == DialogResult.OK)
// 2003一下版本 方法this.axMediaPlayer1.FileName = ofDialog.FileName;
this.axMediaPlayer1.URL = ofDialog.FileName;//2005用法
5.Microsoft speech object Library
///<summary
/// 播放声音文件
/// </summary>
/// <paramname="FileName">文件全名</param>
public voidPlaySound(string FileName)
//要加载COM组件:Microsoft speech object Library
if (!System.IO.File.Exists(FileName))
return;
SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
spFs.Close();
3.设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control,第二个参数是个枚举类型。
在这个程序中,Control我随便弄了个参数塞进去(很汗吧!)。如果在windows程序中,可以用this代替。第二个参数就是优先级别,这里表示优先播放。
4.开辟缓冲区。对于上面的声音设备,他有个自己的缓冲区,叫主缓冲区。系统中,一个设备有唯一的主缓冲区。由于windows是多任务(又是这个!),所以可以有几个程序同时利用一个设备播放声音,所以每个程序都自己开辟一个二级缓冲区,放自己的声音。
系统根据各个程序的优先级别,按照相应的顺序分别去各个二级缓冲区中读取内容到主缓冲区中播放。这里,我们为SND.W***开辟一个缓冲区。
其中,第一个参数表示文件名(傻瓜都看出来了!),第二个就是需要使用的设备。
//dxsnd.cs
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.Windows.Forms;
namespace test1
{
class test
{
public static void Main(string [] args)
{
Device dv=new Device();
dv.SetCooperativeLevel((new UF()),CooperativeLevel.Priority);
SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);
buf.Play(0,BufferPlayFlags.Looping);
Console.ReadLine();
}
class UF:Form{}
}
}
http://blog.csdn.net/holyrong/article/details/1748500
http://www.cnblogs.com/net-study/archive/2013/07/10/3181674.html
http://www.sufeinet.com/thread-459-1-1.html