简体   繁体   English

C#播放嵌入式mp3文件

[英]C# play embedded mp3 file

I know there are a dozen questions like this and I looked at the more popular ones, but I have had no luck in figuring out anything. 我知道有很多这样的问题,我看了一些比较流行的问题,但是我没有运气找出任何问题。

I want to make a C# *.exe that would play a sound when opened, I decided it would be OK to start with a Console Application project, but I have no idea if that is the best choice. 我想制作一个C#* .exe,在打开时会发出声音,我决定从控制台应用程序项目开始就可以了,但是我不知道这是否是最佳选择。

I have added the "file.mp3" file to the resource designer under "Other" files and set its build action to "Embedded Resource". 我已经将“ file.mp3”文件添加到资源设计器的“其他”文件下,并将其生成操作设置为“嵌入式资源”。

From there I saw the suggestion about WMPLib, which I cannot access in my project - it is not present neither in my Reference Manager, neither does "using WMPLib;" 从那里,我看到了有关WMPLib的建议,该建议在我的项目中无法访问-我的引用管理器中既没有该建议,“使用WMPLib”也没有该建议。 work (it raises a "could not be found" error) or the one about the "System.Media.SoundPlayer.Play();" 工作(引发“找不到”错误)或有关“ System.Media.SoundPlayer.Play();”的错误。 method but I am having trouble implementing it, because my complier either says an object reference is required for it or it has no overload, because it only takes 1 arguments if I make it like "System.Media.SoundPlayer.Play("file.mp3");" 方法,但是我在实现它时遇到了麻烦,因为我的编译器要么说需要它一个对象引用,要么没有重载,因为如果我将其设为“ System.Media.SoundPlayer.Play(”文件),则它仅接受1个参数。 MP3 “);”

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Mum();
        }
        void Mum()
        {
            System.Media.SoundPlayer.Play();
        }
    }
}

To play an mp3 file, You will need to add the windows media player Library. 要播放mp3文件,您将需要添加Windows Media Player媒体库。 1. Add a reference to the WMP library - in your solution explorer, under your project, go to References, right click and add a reference to WMP. 1.添加对WMP库的引用-在解决方案资源管理器中的项目下,转到“引用”,右键单击并添加对WMP的引用。 It will be under the COM libraries, I think. 我认为它将在COM库下。 2. Add "using WMPLib;" 2.添加“使用WMPLib;” to your code at the top, 3. Add this code to play the file: 在顶部的代码中,3.添加以下代码以播放文件:

        WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
        myplayer.URL = "mysound.mp3";
        myplayer.controls.play();

mind you the URL is the path to the file you want to play. 请注意,URL是您要播放的文件的路径。 If it has backslashes in it, you will have to "escape" them. 如果其中包含反斜杠,则必须“转义”它们。 Like this 像这样

 myplayer.URL = "C:\\Users\\Public\\Music\\Sample Music\\Kalimba.mp3";

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

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