我正在尝试播放音乐文件,在此期间我想为我的控制台游戏播放一些音效。
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();
}
}