一次播放多个声音文件

Play many soundfiles at once

我正在尝试播放音乐文件,在此期间我想为我的主机游戏播放一些音效。

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();
        }

两者都在工作,但是当我启动 SwordSound 文件时,Battel 音乐停止了。 谢谢大家的帮助:)

尝试在方法内创建播放器,不同的对象,分开,每个声音。如果不同的 SoundPlayer 实例(对于每个声音)不能解决问题,请尝试将其替换为 MediaPlayer ( https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=netframework-4.8 )

您可以尝试使用以下代码在控制台应用程序中同时播放两个声音文件。

   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();
        }
    }

添加参考: