(Solved) How I can play two sounds simultaneously in C#


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.

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();
}

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.

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;
    }
}

Good Luck!

solved How I can play two sounds simultaneously in C#