C#文字冒险音乐问题

时间:2016-10-15 16:29:35

标签: c# audio text adventure

对于学校我必须进行文字冒险,我刚刚创建了菜单。现在我想在菜单中有背景音乐,我有点想办法。然而问题是,当我进入选项菜单时,音乐停止播放。在按下“开始”按钮之前,我希望音乐能够持续播放。

这是我的菜单代码:

class Program
{
    static string Playername = "";
    static string CorrectIntro = "";
    static ConsoleColor TextColor = ConsoleColor.White;

    static System.Threading.Thread MusicPlayer;

    private static SoundPlayer IntroMusic;
    private static SoundPlayer MenuSelector;

    public static void Main(string[] args)
    {

        MusicPlayer = new System.Threading.Thread(new System.Threading.ThreadStart(BackgroundMusic));

        //Menu Selector Sound
        MenuSelector = new System.Media.SoundPlayer();
        MenuSelector.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\MenuSelector.wav";

        Menu();
    }

    public static void BackgroundMusic()
    {
        IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
        IntroMusic.Play();
    }

    private static void Menu()
    {
        IntroMusic = new System.Media.SoundPlayer();
        IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
        IntroMusic.Play();

        Console.ForegroundColor = TextColor;

        Zin("The story of Benedict Henderson");
        Enter('\n');
        Enter('\n');
        Zin("[1] Start");
        Enter('\n');
        Zin("[2] Options");
        Enter('\n');
        Zin("[3] Credits");
        Enter('\n');
        Zin("[4] Quit");
        Enter('\n');

        ConsoleKeyInfo MenuSelector = Console.ReadKey(true);

        switch (MenuSelector.Key)
        {
            case ConsoleKey.D1:
                Intro();
                break;
            case ConsoleKey.D2:
                Options();
                break;
            case ConsoleKey.D3:
                Credits();
                break;
            case ConsoleKey.D4:
                Quit();
                break;
        }

        Console.ReadKey();

    }

    //Quit function
    private static void Quit()
    {
        MenuSelector.Play();
        Environment.Exit(0);
    }

    //Credits function
    private static void Credits()
    {
        MenuSelector.Play();
        Console.Clear();

        Zin("This game is made and designed by Stijn van der Neut");
        Enter('\n');
        Enter('\n');
        Zin("Press enter to return to the menu");

        ConsoleKeyInfo ReturnToMenu = Console.ReadKey(true);

        if (ReturnToMenu.Key == ConsoleKey.Enter)
        {
            Console.Clear();
            Menu();
        }

    }

    //Option function
    private static void Options()
    {
        MenuSelector.Play();
        Console.Clear();



        Zin("Please select the desired text color");
        Enter('\n');
        Enter('\n');
        Zin("[1] White");
        Enter('\n');
        Zin("[2] Red");
        Enter('\n');
        Zin("[3] Blue");
        Enter('\n');
        Zin("[4] Green");
        Enter('\n');
        Zin("[5] Magenta");
        Enter('\n');
        Zin("[6] Cyan");
        Enter('\n');
        Enter('\n');
        Zin("Press enter to return to the menu");

        ConsoleKeyInfo ColorSelector = Console.ReadKey(true);

        //Get player input for ColorSelector
        switch (ColorSelector.Key)
        {
            case ConsoleKey.D1:
                MenuSelector.Play();
                TextColor = ConsoleColor.White;
                break;
            case ConsoleKey.D2:
                MenuSelector.Play();
                TextColor = ConsoleColor.Red;
                break;
            case ConsoleKey.D3:
                MenuSelector.Play();
                TextColor = ConsoleColor.Blue;
                break;
            case ConsoleKey.D4:
                MenuSelector.Play();
                TextColor = ConsoleColor.Green;
                break;
            case ConsoleKey.D5:
                MenuSelector.Play();
                TextColor = ConsoleColor.Magenta;
                break;
            case ConsoleKey.D6:
                MenuSelector.Play();
                TextColor = ConsoleColor.Cyan;
                break;
            case ConsoleKey.Enter:
                MenuSelector.Play();
                Console.Clear();
                Menu();
                break;
            default:
                TextColor = ConsoleColor.Black;
                break;
        }

        Console.ForegroundColor = TextColor;
        Console.Clear();
        Menu();

    }

对于长长的代码块感到抱歉,顺便说一句,对我来说很容易,我刚刚开始编写3个月的编码教育:)。

所以我要澄清一些事情。有2个SoundPlayer对象,一个播放背景音乐,另一个(MenuSelector)在进入子菜单时播放短小的声音。

我将代码更改为此,因为显然play功能会自动为您创建一个新线程,而不必自己创建。但音乐仍然不会在整个菜单中持续播放。

class Program
{
    static string Playername = "";
    static string CorrectIntro = "";
    static ConsoleColor TextColor = ConsoleColor.White;

    private static SoundPlayer IntroMusic;
    private static SoundPlayer MenuSelector;


    public static void Main(string[] args)
    {
        //Menu Selector Sound
        MenuSelector = new System.Media.SoundPlayer();
        MenuSelector.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\MenuSelector.wav";

        Menu();
    }

    private static void Menu()
    {
        IntroMusic = new System.Media.SoundPlayer();
        IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
        IntroMusic.Play();

        Console.ForegroundColor = TextColor;

        Zin("The story of Benedict Henderson");
        Enter('\n');
        Enter('\n');
        Zin("[1] Start");
        Enter('\n');
        Zin("[2] Options");
        Enter('\n');
        Zin("[3] Credits");
        Enter('\n');
        Zin("[4] Quit");
        Enter('\n');

        ConsoleKeyInfo MenuSelector = Console.ReadKey(true);

        switch (MenuSelector.Key)
        {
            case ConsoleKey.D1:
                Intro();
                break;
            case ConsoleKey.D2:
                Options();
                break;
            case ConsoleKey.D3:
                Credits();
                break;
            case ConsoleKey.D4:
                Quit();
                break;
        }

        Console.ReadKey();

    }

    //Quit function
    private static void Quit()
    {
        MenuSelector.Play();
        Environment.Exit(0);
    }

    //Credits function
    private static void Credits()
    {
        MenuSelector.Play();
        Console.Clear();

        Zin("This game is made and designed by Stijn van der Neut");
        Enter('\n');
        Enter('\n');
        Zin("Press enter to return to the menu");

        ConsoleKeyInfo ReturnToMenu = Console.ReadKey(true);

        if (ReturnToMenu.Key == ConsoleKey.Enter)
        {
            Console.Clear();
            Menu();
        }

    }

    //Option function
    private static void Options()
    {
        MenuSelector.Play();
        Console.Clear();


        Zin("Please select the desired text color");
        Enter('\n');
        Enter('\n');
        Zin("[1] White");
        Enter('\n');
        Zin("[2] Red");
        Enter('\n');
        Zin("[3] Blue");
        Enter('\n');
        Zin("[4] Green");
        Enter('\n');
        Zin("[5] Magenta");
        Enter('\n');
        Zin("[6] Cyan");
        Enter('\n');
        Enter('\n');
        Zin("Press enter to return to the menu");

        ConsoleKeyInfo ColorSelector = Console.ReadKey(true);

        //Get player input for ColorSelector
        switch (ColorSelector.Key)
        {
            case ConsoleKey.D1:
                MenuSelector.Play();
                TextColor = ConsoleColor.White;
                break;
            case ConsoleKey.D2:
                MenuSelector.Play();
                TextColor = ConsoleColor.Red;
                break;
            case ConsoleKey.D3:
                MenuSelector.Play();
                TextColor = ConsoleColor.Blue;
                break;
            case ConsoleKey.D4:
                MenuSelector.Play();
                TextColor = ConsoleColor.Green;
                break;
            case ConsoleKey.D5:
                MenuSelector.Play();
                TextColor = ConsoleColor.Magenta;
                break;
            case ConsoleKey.D6:
                MenuSelector.Play();
                TextColor = ConsoleColor.Cyan;
                break;
            case ConsoleKey.Enter:
                MenuSelector.Play();
                Console.Clear();
                Menu();
                break;
            default:
                TextColor = ConsoleColor.Black;
                break;
        }

        Console.ForegroundColor = TextColor;
        Console.Clear();
        Menu();

    }

1 个答案:

答案 0 :(得分:1)

不确定是否修复了它,但您应该查看Windows Media Player库“WMPlib”。您应该能够使用此代码:

Using WMPlib;

private WindowsMediaPlayer player;
private string filename = $"C:\[FILE LOCATION]";

public static void main(string[] args)
{
    player = new WindowsMediaPlayer();
    player.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChanged);
    player.URL = filename;
    PlayBackgroundMusic();
}

private void PlayBackgroundMusic()
{
    player.controls.play();
}

private void Player_PlayStateChanged(int newState)
{
    if ((WMPPlayState)newState == WMPPlayState.wmppsStopped)
    {
        PlayBackgroundMusic();
    }
}

希望这有助于,如果不是这个项目,那么将来。 如果需要手动添加,可以在COM组件或Windows \ system32中找到Windows Media Player库。

另外,请注意该库是为.Net framework 4编写的,因此请确保项目使用框架4作为目标。