-2

I'm just messing around with coding, and I want to know how to play an mp3 file if a user presses a button. I can already detect the user input no problem, it's playing a file that I can't seem to find how to do

    {

        Console.WriteLine("Hello,[user]!");
        Console.WriteLine("Please Enter Your Username");
        Console.WriteLine();
        
        string uname = Console.ReadLine();

        Thread.Sleep(600);
        Console.Clear();
        Thread.Sleep(600);

        Console.WriteLine("Welcome to Axiom, " + uname);
        Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
        Console.WriteLine();

    while (true)
    {

        string keyChoice = Console.ReadLine();
       
        switch (keyChoice)
        {
            case "1":
                Thread.Sleep(600);
                Console.Clear();
                Thread.Sleep(600);
                Console.WriteLine("Console Commands:");
                Console.WriteLine("Press '2' To Get 1000$ FREE");
                Console.WriteLine("Press '3' To Pick Command 3");
                Console.WriteLine();
                Console.WriteLine("Press '0' To Exit Or Press A Command Button To Execute A Command");
                Console.WriteLine();
                continue; 

            case "2":
                Thread.Sleep(600);
                Console.Clear();
                Thread.Sleep(600);
                //This is where I want to add the file. To rickroll someone of course
                continue; `
Blue_A55T
  • 21
  • 4
  • 4
    All that has been posted is a program description, but that doesn't tell us what _problem_ you're having. What have you tried, and what troubles did you encounter? Please [edit] your post to include a [valid question](//stackoverflow.com/help/how-to-ask) that we can answer. Reminder: make sure you know what is [on-topic](//stackoverflow.com/help/on-topic); asking us to write the program for you, suggestions, and external links are off-topic. – gunr2171 Nov 12 '21 at 17:06
  • Does this answer your question? [Playing a MP3 file in a WinForm application](https://stackoverflow.com/questions/15025626/playing-a-mp3-file-in-a-winform-application) – Mark Benningfield Nov 12 '21 at 18:17

1 Answers1

0

If you want to play an mp3 file when a user presses a button, you could refer to the following code:

static void Main()
    {
        Console.WriteLine("Hello,[user]!");
        Console.WriteLine("Please Enter Your Username");
        Console.WriteLine();

        string uname = Console.ReadLine();

        Thread.Sleep(600);
        Console.Clear();
        Thread.Sleep(600);

        Console.WriteLine("Welcome to Axiom, " + uname);
        Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
        Console.WriteLine();
        while (true)
        {

            string keyChoice = Console.ReadLine();
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = "the path of  Your MP3 file.mp3";

            switch (keyChoice)
            {
                case "1":
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    Console.WriteLine("Console Commands:");
                    Console.WriteLine("Press '2' To Get 1000$ FREE");
                    Console.WriteLine("Press '3' To Pick Command 3");
                    Console.WriteLine();
                    Console.WriteLine("Press '0' To Exit Or Press A Command Button To Execute A Command");
                    Console.WriteLine();
                    continue;

                case "2":
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    wplayer.controls.play();
                    continue;

            }
        }
    }

Before that you should follow the steps below to add a reference:

  1. First go to the references of your project
  2. Right click on choose ‘Add References…’
  3. Add the library under COM object for window media player

You can also refer to these pages.

https://learn.microsoft.com/zh-cn/windows/win32/wmp/creating-the-windows-media-player-control-programmatically?redirectedfrom=MSDN

Playing a MP3 file in a WinForm application

Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10