简体   繁体   English

如何在 C# 控制台应用程序中使用音效?

[英]How can I use sound effect in C# console app?

I´m doing this small RPG game in the C# console app, and I wanted to add some background music and effects when choosing menu options.我在 C# 控制台应用程序中做这个小型 RPG 游戏,我想在选择菜单选项时添加一些背景音乐和效果。

What I noticed was that I wasn´t able to do anything when the background music started to play.我注意到当背景音乐开始播放时我什么也做不了。 I thought of threading, but this is completly new to me (started to learn C# 6 weeks ago).我想到了线程,但这对我来说是全新的(6 周前开始学习 C#)。

What I managed to do was starting a new thread and play the sounds我设法做的是开始一个新线程并播放声音

static Thread backgroundMusic = new Thread(() =>
    {

        using (var audioFile = new AudioFileReader(AppDomain.CurrentDomain.BaseDirectory + "\\menu.mp3"))
        using (var outputDevice = new WaveOutEvent())
        {
            backgroundMusic.IsBackground = true;
            outputDevice.Init(audioFile);
            outputDevice.Play();
            while (true)
            {
                Thread.Sleep(1);
            }

        }
    });

And then for the sound effect I do...然后我做的声音效果...

static Thread click = new Thread(() =>
    {

        using (var audioFile = new AudioFileReader(AppDomain.CurrentDomain.BaseDirectory + "\\click.mp3"))
        using (var outputDevice = new WaveOutEvent())
        {
            click.IsBackground = true;
            outputDevice.Init(audioFile);
            outputDevice.Play();
            while (true)
            {
                Thread.Sleep(1);
            }
        }
    });

I start these with click.Start();我从 click.Start(); 开始。 backgroundMusic.Start();背景音乐.Start();

Ok so far so good.好的,到目前为止一切都很好。 It plays the background music and it plays the sound effect, but only one time.它播放背景音乐和播放音效,但只有一次。 Can I reuse the thread in some way to play the click sound again when another option is chosen? Can I reuse the thread in some way to play the click sound again when another option is chosen? And can I abort sound in some way?我可以以某种方式中止声音吗? I might want different music when you play the game and in the menus.当你玩游戏和菜单时,我可能想要不同的音乐。

tried backgroundMusic.Abort();试过 backgroundMusic.Abort(); but then I got this:但后来我得到了这个:

System.PlatformNotSupportedException: 'Thread abort is not supported on this platform.'

And i can not restart a thread once I´ve started it one time.一旦我启动了一次线程,我就无法重新启动它。 I tried with backgroundMusic.Start();我试过 backgroundMusic.Start();

I´ve been checking out forums but all seems to cover windows forms, and not be working with console app.我一直在查看论坛,但似乎都涵盖了 windows forms,而不是使用控制台应用程序。

https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-5.0 I´ve also checked out the documentation... but honestly I think the documentation at microsoft is NOT for beginners. https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-5.0我还检查了文档...但老实说,我认为微软的文档不是给菜鸟的。 I find it very hard to understand.我觉得很难理解。

I´ve might have been doing it all wrong, so don´t be hard on me, but please come with suggestions how I can improve.我可能一直做错了,所以不要对我太苛刻,但请提出如何改进的建议。

So I want: Background music playing and looping Click sound every time you choose a menu option所以我想要: 播放和循环播放背景音乐 每次选择菜单选项时单击声音

I have: Background music playing once (til the end of the file) Click sound on the first menu option, there after it throws an exception (see above)我有:背景音乐播放一次(直到文件末尾)单击第一个菜单选项上的声音,然后引发异常(见上文)

You should never, ever use Thread.Abort() .你永远不应该使用Thread.Abort() It just stops the thread in an "evil" way - by throwing an exception, and you never know what side effects that will have.它只是以一种“邪恶”的方式停止线程——通过抛出异常,你永远不知道会有什么副作用。

You need CancellationToken .你需要CancellationToken Check out this article: https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads查看这篇文章: https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads

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

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