我希望能够在播放音乐时按下按钮来播放按下按钮的声音。 SoundPlayer 没有帮助,因为它停止了音乐播放声音。 我已将声音添加到我的资源 (WindowsFormsApplication2.Resources.Properties.button),我不想输入文件位置。
最佳答案
来自 MerickOWA 的回答
You CANNOT play two sounds at once using SoundPlayer.
SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.
There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)
The simplest option would be to depend on windows media player to play the audio for you.
Add a reference to "C:\Windows\System32\wmp.dll"
then use
var player = new WMPLib.WindowsMediaPlayer(); player.URL = @"..\..\bin\debug\tribal dance.wav";
Note: play starts immediately after setting the URL property
The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.
关于C# 从您的资源中一次播放多个音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086058/