繁体   English   中英

如何在 Visual Studio 中在 c# 上播放 mp3 文件

[英]How to play an mp3 file on c# in visual studio

我只是在搞代码,我想知道如果用户按下按钮如何播放 mp3 文件。 我已经可以检测到用户输入没问题,它正在播放一个我似乎找不到怎么做的文件

    {

        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; `

如果您想在用户按下按钮时播放 mp3 文件,可以参考以下代码:

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;

            }
        }
    }

在此之前,您应该按照以下步骤添加引用:

  1. 首先去你的项目的参考资料
  2. 右键单击选择“添加引用...”
  3. 为window media player添加COM对象下的库

您也可以参考这些页面。

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

在 WinForm 应用程序中播放 MP3 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM