Search code examples
c#audiomedia-player

Play many soundfiles at once


I am trying to play a music file and during that I want to play some soundeffects for my console game.

SoundPlayer player = new SoundPlayer();
public static void StartBattelMusic()
        {
            player.SoundLocation = "D:....BattelMusic.wav";
            player.Play();
        }


        public static void SwordHitSound()
        {
            player.SoundLocation = "D:....SwordHitSound.wav";
            player.Play();
        }

Both are working, but when I start the SwordSound file the Battel music stops. Thanks you allready for helping :)


Solution

  • You could try the following code to play two soundfiles in console app at once.

       class Program
        {
    
            static void Main(string[] args)
            {
                string path1 = "D:\\test.mp3";
                string path2 = "D:\\1.mp4";
                play(path1);
                play(path2);
                Console.ReadKey();
            }
            static  void play(string audioPath)
            {
                MediaPlayer myPlayer = new MediaPlayer();
                myPlayer.Open(new System.Uri(audioPath));
                myPlayer.Play();
            }
        }
    

    Add reference: enter image description here