如何在 C# 中同时播放两个声音 [英] How I can play two sounds simultaneously in C#

查看:136
本文介绍了如何在 C# 中同时播放两个声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2017 上使用 C# 创建平台游戏.如何同时播放两种声音?

I'm creating platform game using C# on Visual Studio 2017. How I can play two sounds simultaneously?

我试过这个:同时播放两种声音,但没有奏效.

I have tried this: Play two sounds simultaneusly , but it didn't work.

这是播放音乐的代码 -

Here is the code for playing the music -

private void Bg_music()
{
    new System.Threading.Thread(() =>
    {
        var bg = new System.Windows.Media.MediaPlayer();
        bg.Open(new System.Uri(path + "Foniqz_-_Spectrum_Subdiffusion_Mix_real.wav"));
        bg.Play();
    }).Start();                        
}

Sound_0,这应该一直播放Sound_1,这应该只在击中硬币时播放,而 Sound_0 正在播放

Sound_0, this should play all the time Sound_1, this should play only when hitting coin, while Sound_0 is playing

推荐答案

我在你的代码中发现的第一个问题是你在 Timer1_Tick 中调用了 Bg_music(),其中是错的.每次,在计时器滴答时,都会创建一个不正确的新线程.

First problem which I found in your code is you are calling Bg_music() in Timer1_Tick, Which is wrong. Everytime, At time of timer tick a new thread is created which is not correct.

除此之外,您还使用了 var bg,其范围仅限于 Bg_music() 的方法.您应该使用 MediaPlayer 而不是 var 并且您的 MediaPlayer bg 应该位于表单的顶层(全局).就像 -

Apart from that, You have used var bg whose scope is limited to that method of Bg_music() only. You should use MediaPlayer instead of var and Your MediaPlayer bg should be at top level of form (Global). It would be something like -

MediaPlayer bg;

public game_form()
{
    InitializeComponent();
    Bg_music(); //Calling the background music thread at the time user start playing the game.

    path = Directory.GetCurrentDirectory();
    path = path + "\\..\\..\\Resources\\";
    Aanet_checking();
    Translate();
    Character_checking();
}

你的 Bg_music() 看起来像这样 -

Your Bg_music() will look something like this -

private void Bg_music()
{
    new System.Threading.Thread(() =>
    {
        bg = new System.Windows.Media.MediaPlayer();
        bg.Open(new System.Uri(path + "Foniqz_-_Spectrum_Subdiffusion_Mix_real.wav"));
        bg.Play();
    }).Start();                        
}

此更改肯定会解决您的问题.

This change will definitely solve your problem.

除了这个问题,我观察到的是大量的图形闪烁.您应该启用双缓冲来摆脱那些闪烁的问题.这将使您的游戏体验流畅无闪烁.

Apart from this problem what I observed is a lot of graphical flickering. You should enable Double Buffering to get rid of those flickering issue. This will make your game experience smooth without flickering.

双缓冲的作用是先在内存中在后台创建 UI,然后一次性显示图像.这提供了不间断的图形输出.只需将以下代码复制并粘贴到您的表单中 -

What double buffering does is create the UI in the memory first in the background and then display the image at one shot. This gives graphics output without interruption. Just copy and paste the below code in your form -

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED //Enable double buffering
        return cp;
    }
}

祝你好运!

这篇关于如何在 C# 中同时播放两个声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆