声音文件folder.wav放置在bin目录下debug下
1.通过API调用
- using System.Runtime.InteropServices;
- [DllImport("winmm.dll")]
- public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);
- public const int SND_FILENAME = 0x00020000;
- public const int SND_ASYNC = 0x0001;
- //调用下面的方法
- PlaySound("folder.wav", 0, SND_ASYNC | SND_FILENAME);
2.通过SoundPlayer对象
- CODE:
- using System.Media;
- SoundPlayer p = new SoundPlayer();
- p.SoundLocation = Application.StartupPath + "//folder.wav";
- p.Load();
- p.Play();
SoundPlayer
支持同步、异步播放
支持循环播放
支持文件和流播放
同步播放:
- System.Media.SoundPlayer player = new System.Media.SoundPlayer();
- player.SoundLocation = Application.StartupPath + "//folder.wav";
- player.Load();
- player.Play();
异步播放:
- System.Media.SoundPlayer player = new System.Media.SoundPlayer();
- player.SoundLocation = Application.StartupPath + "//folder.wav";
- player.LoadAsync();
- player.PlaySync();
循环播放
- System.Media.SoundPlayer player = new System.Media.SoundPlayer();
- player.SoundLocation = Application.StartupPath + "//folder.wav";
- player.Load();
- player.PlayLooping();
3.使用 Audio 对象
- using Microsoft.VisualBasic.Devices;//Microsoft.VisualBasic.dll需要添加引用
- Audio a = new Audio();
- a.Play("folder.wav");