简体   繁体   English

同时播放 2 个声音

[英]Playing 2 sounds together at same time

Designing a game for any laptop:为任何笔记本电脑设计游戏:

I want to play 2 sounds at the same time!我想同时播放 2 个声音! A background music and a sound when pressing on a button!按下按钮时的背景音乐和声音!

Using System.Media won't allow me to play 2 sounds together.使用System.Media不允许我同时播放 2 个声音。 For background music, I used the following:对于背景音乐,我使用了以下内容:

SoundPlayer sp = new SoundPlayer(@"..\..\bin\debug\tribal dance.wav");
sp.Play;

If I played another SoundPlayer , the previous one stops!如果我播放另一个SoundPlayer ,则前一个停止!

I want the background music to keep playing until the form is closed!我想让背景音乐一直播放到表单关闭! And at the same time, if a button is clicked, another sound will be played with the music!同时,如果单击一个按钮,则会随着音乐播放另一种声音!

I also tried added the Windows Media Player to the toolbox and used it (allowing me 2 play 2 sounds at same time).我还尝试将 Windows Media Player 添加到工具箱并使用它(允许我同时播放 2 个声音)。 That works fine but the path will give an error if I put it (@""..\\..\\bin\\debug\\tribal dance.wav"); and I need to put it that way in order to be read by any computer.这工作正常,但如果我把它放在路径上会出错(@""..\\..\\bin\\debug\\tribal dance.wav");我需要这样写才能被任何人阅读电脑。

Any suggestions?有什么建议么?

You CANNOT play two sounds at once using SoundPlayer.您不能使用 SoundPlayer 一次播放两个声音。

SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. SoundPlayer 使用本机 WINAPI PlaySound函数来完成不支持同时播放声音的任务。 Creating multiple instances of SoundPlayer won't help.创建 SoundPlayer 的多个实例无济于事。

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)有很多选项,其中大部分涉及为窗口的本机音频 DirectSound实现许多低级 API(注意,C# 都不是,并且需要大量互操作代码)

The simplest option would be to depend on windows media player to play the audio for you.最简单的选择是依靠 Windows 媒体播放器为您播放音频。

Add a reference to "C:\\Windows\\System32\\wmp.dll"添加对“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注意:设置 URL 属性后立即开始播放

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.从好的方面来说,您可以使用任何文件格式的媒体播放器支持。

Using (WMPLib) Windows Media Player Library you can do this easily.使用 (WMPLib) Windows Media Player Library 您可以轻松地做到这一点。 Keep Your file in Debug/Sound folder and follow the following code.将您的文件保存在 Debug/Sound 文件夹中并遵循以下代码。 This will run 2 file simultaneously.这将同时运行 2 个文件。 To Add WMPLib in your project add it by NuGet Package Manager.要在项目中添加 WMPLib,请通过 NuGet 包管理器添加它。

 WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
 WMPLib.WindowsMediaPlayer wplayer2 = new WMPLib.WindowsMediaPlayer();     
 string play_string = "be.mp3";
 wplayer.URL = @"SOUND/" + play_string;
 wplayer.controls.play();
 string play_string2 = "ba.mp3";
 wplayer2.URL = @"SOUND/" + play_string2;
 wplayer2.controls.play();

http://msdn.microsoft.com/en-us/library/dd562851%28v=VS.85%29.aspx这篇文章可能会有所帮助如果您需要文件的绝对路径,您可以使用下一个代码

string soundFile = Path.Combine(Directory.GetCurrentDirectory() + "file.wav");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM