-3

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, this should play all the time Sound_1, this should play only when hitting coin, while Sound_0 is playing

Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39
Miicat
  • 49
  • 5
  • 3
    please provide some code regarding what you have tried. – Arpit Gupta May 19 '18 at 12:33
  • I dont find any problem in playing two sounds according to the link you provided. Can you tell what the exact problem you facing? – Arpit Gupta May 19 '18 at 12:39
  • Here's link to my project https://github.com/miicat/jumping-google in game_form.cs in line 238, I have commented out the sound I called Sound_0. It plays when I start the program, but stops playing when I move player – Miicat May 20 '18 at 07:57
  • "It didn't work" tells us nothing that we can use to help you. Help us help you! – Eric Lippert May 21 '18 at 17:41
  • @EricLippert I have tried his application from the code he provided. Also got his problem and posted the answer below. – Arpit Gupta May 21 '18 at 17:57

1 Answers1

1

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!

Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39