0

i am making a quiz and in each form i have background music and if they get an answer correct i want to play a sound effect. When i do this is stops the background music and plays the sound effect then no sound is playing, does anyone know how do it?

public static System.Media.SoundPlayer player = new System.Media.SoundPlayer();

public static void sound(string form)
{
    switch (form)
    {
        case "Login":
            player.Stop();
            player.Stream = Properties.Resources._2marioloadscreen;
            player.PlayLooping();
            break;
     }
  }
RyanJM
  • 19
  • 5
  • Thank you for taking the time to share your question. There is something missing. What is your goal & difficulty? What have you done so far? Please try to better explain your issue, dev env, data types & expected result, as well as to share more or less code (no screenshot), some samples, images or sketches of screens, & user stories or scenario diagrams. To help you improve your requests, please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) & [Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question). –  Dec 15 '20 at 00:32
  • Are you using the `System.Media.SoundPlayer.Play()` which stops the current sound to play a new one, or are you using something else? –  Dec 15 '20 at 00:32
  • Hello Oliver, thanks for your time for responding, i honestly haven't tried anything else yet, as i can't find anywhere that resolves me issue, i am looping my sounds if that changes the code, I am creating a soundplayer, streaming the sound and using soundplayer.play, i will add the code below – RyanJM Dec 15 '20 at 00:38

1 Answers1

0

Here's an example of how to do it, assuming you have two wav files (one for background music, and one for the correct answer sound effect). The key is to know when the sound effect is done playing, then continue with the background music again.

I wrote it as a simple C# Console App.

class Program
{
    private static SoundPlayer player;

    static void Main(string[] args)
    {
        using (player = new SoundPlayer())
        {
            player.SoundLocation = "bkgnd.wav";
            player.PlayLooping();
            Console.WriteLine("bkgnd.wav is now playing in a loop. (Hit ENTER key to start the hoorayyouwon.wav)");
            Console.ReadLine();
            Console.WriteLine("hoorayyouwon.wav is now playing.");
            player.Stop();
            player.SoundLocation = "hoorayyouwon.wav";
            player.PlaySync(); // this ensures the hoorayyouwon.wav plays completely before it gets to the next line of code.
            Console.WriteLine("bkgnd.wav is now continuing (actually, starting over). (Hit ENTER key to exit)");
            player.SoundLocation = "bkgnd.wav";
            player.PlayLooping();
            Console.ReadLine();
        }
    }
}
Tam Bui
  • 2,940
  • 2
  • 18
  • 27
  • Hello, thanks for your response, I am wanting to play both at the exact same time instead of stopping one, playing a sound effect then immediately playing again – RyanJM Dec 15 '20 at 00:40
  • Then you cannot use SoundPlayer. It's very old and not a good player: See [here](https://stackoverflow.com/questions/1285294/play-multiple-sounds-using-soundplayer), [here](https://social.msdn.microsoft.com/Forums/windows/en-US/d5e44522-43fe-491d-b59d-f7c95793a8bf/playing-multiple-sounds-simultaneously-with-soundplayer?forum=winforms). I've read MediaPlayer might meet your needs, but haven't looked into it. – Tam Bui Dec 15 '20 at 00:46
  • Thanks for your assistance, although i have already looked into that page and it doesn't make sense to me, for clarification, I am new to c# and I am currently coding a quiz for my course in school...I have heard sound player does not give access to playing 2 sounds at once but I don't know how to use media player for this function – RyanJM Dec 15 '20 at 00:51