0

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

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Dominik
  • 300
  • 1
  • 14
  • Check the Answer to the same problem. https://stackoverflow.com/questions/1285294/play-multiple-sounds-using-soundplayer – mail2subhajit Nov 05 '19 at 21:48

2 Answers2

2

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

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • Does it need to be MediaPlayer, because I get the compiler error, missing using direktive.... – Dominik Nov 06 '19 at 12:50
  • 1
    I only add using System.Windows.Media; this code to use the MediaPlayer. What error do you get, could you describe it more clearly? – Jack J Jun Nov 07 '19 at 07:14
  • The Type oder the Namespace "Media" is not inside the "System.Windows" namespace.... I´m working in an Console-App(.Net Framework), maybe because of the wrong Project? – Dominik Nov 07 '19 at 17:06
  • 1
    My test environment is also Console-app. Besides, I also updated my reply, which could tell you how to add reference. – Jack J Jun Nov 08 '19 at 02:06
  • Ok.. I see however that is not poping up, maybe I am missing some speacial tools or extentions or should I install something in the VisualStudio Installer? – Dominik Nov 08 '19 at 08:43
  • 1
    You can also add the dll reference from the path(C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\PresentationCore.dll). Please try it again. – Jack J Jun Nov 11 '19 at 01:35
0

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 )

StefanaB
  • 184
  • 2
  • 11
  • How can I use that in a Console application?^^ – Dominik Nov 05 '19 at 16:10
  • public static void StartBattelMusic() { SoundPlayer player = new SoundPlayer(); player.SoundLocation = "D:....BattelMusic.wav"; player.Play(); } public static void SwordHitSound() { SoundPlayer player = new SoundPlayer(); player.SoundLocation = "D:....SwordHitSound.wav"; player.Play(); } – StefanaB Nov 06 '19 at 11:33