2

For school I have to make a text adventure and I have just created the menu. Now I want to have background music whilst in the menu and I have kind of figured out how. The problem is however, when I, for example, go to the options menu, the music stops playing. What I want it for the music to continuously play until the 'start' button is pressed.

Here is my menu code:

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

    }

Sorry for the long block of code, and by the way, take it easy on me, I just started my coding education for 3 months now :).

So I to clarify things. There are 2 SoundPlayer objects, one that plays the background music and the other (MenuSelector) that plays a short little sound when you go to a submenu.

I changed my code to this because apparently the play function automatically creates a new thread for you instead of having to do it yourself. The music still doesn't continuously play throughout the entire menu though.

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

    }
double-beep
  • 5,031
  • 17
  • 33
  • 41
S. Neut
  • 43
  • 5
  • Looks like you have too many 'new SoundPlayer` and `Play` statements. According to the documentation, you need only one `SoundPlayer` object. When you need to play sound, call `Stop` method, set the desired `SoundLocation` and call `Play` or `PlayLooping` methods (they both use separate thread). – Ivan Stoev Oct 15 '16 at 17:20
  • I created a new thread for the playing of the sound but the play function already creates a new thread by itself is it not? So where would I have to place the play function to make it play through the entire menu? – S. Neut Oct 15 '16 at 17:25

1 Answers1

1

Not sure if you got it fixed but you should have a look into the Windows Media Player library "WMPlib". You should be able to use this code:

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

Hopefully this helps, if not for this project then for the future. The library for Windows Media Player can be found in COM components or at windows\system32 if you need to manually add it.

Also, note that the library is written for .Net framework 4 so make sure in properties that the project is using framework 4 as the target.

Tom McKeown
  • 167
  • 7