C#在VS2019中使用DirectSound播放WAV音频文件

C#在 VS2019 中玩音频还挺方便的,尤其是用 DirectSound 来搞定.wav 文件的播放,算是比较经典的老方法了。你不用费劲去找依赖,库文件都给你准备好了,直接引进来就能跑。

DirectSound 的音频播放,在桌面开发里还是蛮实用的,是对延迟要求不高的场景,比如提示音、背景音这些。核心就是通过SecondaryBuffer加载.wav文件,用Play()一声令下,音频就走起来了。

库文件方面已经贴心地包含了Microsoft.DirectX.DirectSound.dllMicrosoft.DirectX.dll,不用你满网找资源。只要把它们引进项目里,添加下引用,就能愉快地调用 API 了,整个过程不算复杂,逻辑也比较清晰。

哦对了,如果你在搞VS2019相关的项目,顺手看看这些也不错:

如果你在开发过程中遇到音频播放卡顿、格式兼容问题,建议先确认.wav是 PCM 格式的,再看是否需要用FormatConversionProvider做下,DirectSound 对格式还是有点挑的。

,这个示例对入门音频开发还是挺友好的。如果你正好用 C#、又是在搞 WinForm 或 WPF,不妨试试,体验下 Windows 平台下的底层音频控制。

zip
WindowsFormsApp1.zip 预估大小:33个文件
folder
WindowsFormsApp1 文件夹
folder
.vs 文件夹
folder
WindowsFormsApp1 文件夹
folder
v16 文件夹
file
.suo 110KB
folder
Server 文件夹
folder
sqlite3 文件夹
file
storage.ide 636KB
folder
db.lock 文件夹
file
aaa.wav 68KB
folder
bin 文件夹
folder
Debug 文件夹
file
WindowsFormsApp1.pdb 28KB
file
Microsoft.DirectX.DirectSound.dll 174KB
file
Microsoft.DirectX.dll 218KB
file
WindowsFormsApp1.exe.config 231B
file
WindowsFormsApp1.exe 8KB
file
Form1.Designer.cs 2KB
file
Program.cs 528B
folder
obj 文件夹
folder
Debug 文件夹
file
WindowsFormsApp1.csproj.FileListAbsolute.txt 1KB
file
WindowsFormsApp1.csproj.GenerateResource.cache 1012B
file
WindowsFormsApp1.csproj.CoreCompileInputs.cache 42B
file
WindowsFormsApp1.Properties.Resources.resources 180B
file
DesignTimeResolveAssemblyReferencesInput.cache 7KB
folder
TempPE 文件夹
file
WindowsFormsApp1.csprojAssemblyReference.cache 8KB
file
WindowsFormsApp1.pdb 28KB
file
WindowsFormsApp1.Form1.resources 180B
folder
WindowsFormsApp1.csproj.CopyComplete 文件夹
file
WindowsFormsApp1.exe 8KB
folder
Libs 文件夹
file
Microsoft.DirectX.DirectSound.dll 174KB
file
Microsoft.DirectX.dll 218KB
file
WindowsFormsApp1.csproj 4KB
file
Form1.cs 1KB
file
Form1.resx 6KB
file
WindowsFormsApp1.sln 1KB
file
App.config 231B
folder
Properties 文件夹
file
Resources.resx 5KB
file
Settings.settings 249B
file
AssemblyInfo.cs 1KB
file
Settings.Designer.cs 1KB
file
Resources.Designer.cs 3KB
zip 文件大小:514.04KB