【问题标题】:Play many soundfiles at once一次播放多个声音文件
【发布时间】:2019-11-05 15:52:12
【问题描述】:

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

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 音乐停止了。 谢谢大家的帮助:)

【问题讨论】:

标签: c# audio media-player


【解决方案1】:

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

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

添加参考:

【讨论】:

  • 它需要是MediaPlayer,因为我得到编译器错误,缺少使用direktive....
  • 我只使用 System.Windows.Media 添加;此代码使用 MediaPlayer。你遇到什么错误,你能描述得更清楚吗?
  • 命名空间“Media”的类型不在“System.Windows”命名空间内....我在控制台应用程序(.Net Framework)中工作,可能是因为错误项目?
  • 我的测试环境也是Console-app。另外,我还更新了我的回复,可以告诉你如何添加参考。
  • 您也可以从路径(C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\PresentationCore.dll)添加dll引用。请再试一次。
【解决方案2】:

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

【讨论】:

  • 如何在控制台应用程序中使用它?^^
  • public static void StartBattelMusic() { SoundPlayer player = new SoundPlayer(); player.SoundLocation = "D:.... BattelMusic.wav";播放器.Play(); } 公共静态无效 SwordHitSound() { SoundPlayer player = new SoundPlayer(); player.SoundLocation = "D:....SwordHitSound.wav";播放器.Play(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多