Dominik
Dominik

Reputation: 299

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 :)

Upvotes: 0

Views: 108

Answers (2)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

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

Upvotes: 2

StefanaB
StefanaB

Reputation: 183

try to create the player inside the method, different objects, separate, for each sounds. if different SoundPlayer instances (for each sounds) doesn't fix the problem, try to replace it with MediaPlayer ( https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=netframework-4.8 )

Upvotes: 0

Related Questions