简体   繁体   English

如何使用声音播放器从属性中播放多个声音文件

[英]How to play Multiple sound files from the properties using Sound Player

i am making a quiz and in each form i have background music and if they get an answer correct i want to play a sound effect.我正在做一个测验,在每种形式中我都有背景音乐,如果他们得到正确的答案,我想播放声音效果。 When i do this is stops the background music and plays the sound effect then no sound is playing, does anyone know how do it?当我这样做是停止背景音乐并播放音效然后没有声音播放,有人知道怎么做吗?

public static System.Media.SoundPlayer player = new System.Media.SoundPlayer();

public static void sound(string form)
{
    switch (form)
    {
        case "Login":
            player.Stop();
            player.Stream = Properties.Resources._2marioloadscreen;
            player.PlayLooping();
            break;
     }
  }

Here's an example of how to do it, assuming you have two wav files (one for background music, and one for the correct answer sound effect).这是一个如何做的例子,假设你有两个wav文件(一个用于背景音乐,一个用于正确答案音效)。 The key is to know when the sound effect is done playing, then continue with the background music again.关键是要知道音效何时播放完毕,然后再继续播放背景音乐。

I wrote it as a simple C# Console App.我把它写成一个简单的 C# 控制台应用程序。

class Program
{
    private static SoundPlayer player;

    static void Main(string[] args)
    {
        using (player = new SoundPlayer())
        {
            player.SoundLocation = "bkgnd.wav";
            player.PlayLooping();
            Console.WriteLine("bkgnd.wav is now playing in a loop. (Hit ENTER key to start the hoorayyouwon.wav)");
            Console.ReadLine();
            Console.WriteLine("hoorayyouwon.wav is now playing.");
            player.Stop();
            player.SoundLocation = "hoorayyouwon.wav";
            player.PlaySync(); // this ensures the hoorayyouwon.wav plays completely before it gets to the next line of code.
            Console.WriteLine("bkgnd.wav is now continuing (actually, starting over). (Hit ENTER key to exit)");
            player.SoundLocation = "bkgnd.wav";
            player.PlayLooping();
            Console.ReadLine();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM