添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
越狱的排球  ·  日期與時間函數·  1 年前    · 
打篮球的甘蔗  ·  visual studio ...·  2 年前    · 
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
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"D:\test.wav";
player.Load(); //同步加载声音
player.Play(); //启用新线程播放
//player.PlayLooping(); //循环播放模式
//player.PlaySync(); //UI线程同步播放
使用API函数播放
using System.Runtime.InteropServices;
public static class WavPlayer
    [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();
使用directX 

准备工作:

  1.安装了DirectX SDK(有9个DLL文件)。这里我们只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll

  2.一个W***文件。(这样的文件比较好找,在QQ的目录里就不少啊。这里就不多说了。)名字叫SND.W***,放在最后目标程序的同个目录下面

  开始写程序啦。随便用个UltraEdit就好了。

  1.引入DirectX 的DLL文件的名字空间:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
  2.建立设备。在我们导入的Microsoft.DirectX.DirectSound空间中,有个Device的类。这个是表示系统中的声音设备。
Device dv=new Device();
  3.设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control,第二个参数是个枚举类型。   在这个程序中,Control我随便弄了个参数塞进去(很汗吧!)。如果在windows程序中,可以用this代替。第二个参数就是优先级别,这里表示优先播放。 
dv.SetCooperativeLevel((new UF()),CooperativeLevel.Priority);
  4.开辟缓冲区。对于上面的声音设备,他有个自己的缓冲区,叫主缓冲区。系统中,一个设备有唯一的主缓冲区。由于windows是多任务(又是这个!),所以可以有几个程序同时利用一个设备播放声音,所以每个程序都自己开辟一个二级缓冲区,放自己的声音。   系统根据各个程序的优先级别,按照相应的顺序分别去各个二级缓冲区中读取内容到主缓冲区中播放。这里,我们为SND.W***开辟一个缓冲区。   其中,第一个参数表示文件名(傻瓜都看出来了!),第二个就是需要使用的设备。
SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);
  5.接下来就可以播放啦。第一个参数表示优先级别,0是最低的。第2个参数是播放方式,这里是循环播放。
buf.Play(0,BufferPlayFlags.Looping);
  6.由于命令行程序没有消息循环,执行完代码就退出了,所以,我们需要暂停程序。
Console.Read();
  7.关键的部分已经完了,这里只是交代一下刚才的那个倒霉的new UF() 是什么东西。这个完全是为了应付SetCooperativeLevel的参数要求。我不知道这样做有什么附作用(各位如果因此把声卡烧了…………)
class UF:Form{}
  8.代码写完啦~~~。下面可以编译了,这里编译比较复杂点。
csc /r:directX/MicroSoft.DirectX.dll;directX/Microsoft.Directx.DirectSound.dll dxsnd.cs
  这里,我把2个DLL文件放在当前目录的directx目录下(这个是我自己建的,你只需要指出这2个文件的位置就可以了。)   顺便把我的目录结构说明一下: |--dxsnd.cs |--snd.wav |--<directx> |--MicroSoft.DirectX.dll |--Microsoft.Directx.dll   下面是完整代码:
//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

C#中声音的播放主要有六种方法:1.播放系统事件声音 2.使用SoundPlayer3.使用API函数播放4.使用axWindowsMediaPlayer的COM组件来播放5.Microsoft speech object Library6.使用directX1.播放系统事件声音 System.Media.SystemSounds.Asterisk.Pl
第一种是利用DirectX 1.安装了DirectX SDK(有9个DLL文件)。这里我们只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll 2.引入DirectX 的DLL文件的名字空间:  using Microsoft.DirectX;  using Microsoft.DirectX.DirectSound; 3.建立设备 Device dv=new Device(); 4.设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的 SecondaryBuffer buf=new
  在使用C#进行录音和播放录音功能上,使用NAudio是个不错的选择。   NAudio是个开源,相对功能比较全面的类库,它包含录音、播放录音、格式转换、混音调整等操作,具体可以去Github上看看介绍和源码,附:Git地址   我使用到的是录制和播放wav格式的音频,对应调用NAudio的WaveFileWriter和WaveFileReader类进行开发,从源码上看原理就是 百度查了一圈,NAudio相关中文资料较少。鉴于本人最近在使用此库的播放音频方面有所涉及,在此将自己的学习过程与经验总结与大家分享,丰富.NET圈相关知识,同时也欢迎大佬探讨和指正。 为什么使用NAudio NAudio为.NET平台下的开源库,采用ML-PL协议,开源地址:https://github.com/naudio/NAudio。截至今日,已有约2.3k的stars。 NAu...
to clipboardprint? using System.Media;       SoundPlayer sound = new SoundPlayer("声音.wav");   sound.Play();   using System.Media; SoundPlayer sound // 定义一个方法,用于释放非托管资源 [DllImport("Kernel32")] private extern static void CloseHandle(IntPtr handle); 2. 在需要使用该类的地方,使用using语句来自动调用Dispose方法: using (var myClass = new MyClass()) // 使用myClass对象 当using语句结束时,会自动调用myClass对象的Dispose方法。 3. 手动调用Dispose方法: var myClass = new MyClass(); // 使用myClass对象 myClass.Dispose(); 手动调用Dispose方法可以立即释放对象所占用的资源,而不需要等待垃圾回收器执行清理操作。 总之,使用Dispose方法可以确保对象所占用的资源得到及时释放,从而提高程序的性能和稳定性。