简体   繁体   English

有什么办法可以让我在 c# 控制台应用程序中同时播放两个声音?

[英]Is there any way i can somehow have two sounds play at once in c# console app?

Im working on a text game in c#.我正在用 C# 开发一个文本游戏。 But i wanted to have multiple sounds at once.但我想一次有多个声音。 So i tried to use SoundPlayer.所以我尝试使用 SoundPlayer。 But whenever i loaded up another sound, it just cut off the other one.但是每当我加载另一个声音时,它就会切断另一个声音。 If that makes sense.如果这是有道理的。 Im new to programming, so please try to explain like im 5!我是编程新手,所以请尝试像 im 5 一样解释!

If you have say 2 different sounds that you want to play together.如果您说要一起演奏 2 种不同的声音。

Below example is for Console.Beep, which can be extended to other sounds下面的例子是Console.Beep,可以扩展到其他声音

You can replace Console.Beep() with other sounds that you want to play您可以将 Console.Beep() 替换为您想播放的其他声音


using System.Threading;
using System.Threading.Tasks;

Add above 2 to your Namespace将以上 2 添加到您的命名空间

//First sound you want to play

var first = await Task.Run(() => Console.Beep());

//Optional Delay
await Task.Delay(500); //Does not halt the current thread

//Second sound you want to play
var second = await Task.Run(() => Console.Beep(500,1000));

I had been struggling with the same issue.....我一直在为同样的问题而苦苦挣扎......

The short answer is you can't.简短的回答是你不能。 The system will only permit one channel to the sound interface within any given process.在任何给定的过程中,系统将只允许一个通道进入声音接口。 As such, when you start a new sound, the previous one terminates.因此,当您开始一个新的声音时,前一个将终止。

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

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