Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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();
            
        }
Posted
Updated 17-Oct-16 8:22am
v2

1 solution

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

C#
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.
 
Share this answer
 
v2
Comments
Stijn.N 17-Oct-16 15:36pm    
I actually did bother to google this. Alot as a matter of fact. I just didn't understand any of it since I have only started my programming education for 3 months. I do appreciate your answer but I think the last comment is not relevant and not necessary.

If you would be so kind as to explain the code you submitted?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900