C#文本冒险 - 菜单上的声音问题 [英] C# text adventure - sound issues at the menu

查看:63
本文介绍了C#文本冒险 - 菜单上的声音问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello

I have to write a text adventure for school in C# in visual studio 2015. I have made a little starting menu with some options. While the menu is displayed some background music is playing. When you choose an option from the menu a little sound is played but it stops the background music from playing. 

I basically want to play multiple sounds at the same time.

Any help would be appreciated.





我尝试过:





What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Media;

namespace Text_Adventure2
{


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

推荐答案

SoundPlayer无法同时播放两种声音。你将不得不做更多的工作:



SoundPlayer cannot play two sounds at the same time. You're gonna have to do more work:

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"Path_to\myfile1.wav"));
        c.Play();
    }).Start();

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"Path_to\myfile2.wav"));
        c.Play();
    }).Start();





一个不太精心设计的谷歌搜索会发现一些可能的解决方案,如果你有困难尝试。



A not-too-carefully crafted google search would have revealed a number of possible solutions, if you had bothered to try.


这篇关于C#文本冒险 - 菜单上的声音问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆