简体   繁体   English

C#使用资源中的NAudio播放MP3文件

[英]C# Play MP3 file using NAudio from Resource

I have a Windows Forms Application where I'm trying to simply play an MP3 file from the Resource using the NAudio library. 我有一个Windows窗体应用程序,我试图使用NAudio库从资源中简单地播放MP3文件。

I believe what needs to be done is to stream the file somehow, here is an example of NAudio, unfortunately it accepts File path string as an argument. 我相信需要做的是以某种方式流式传输文件,这是一个NAudio的例子,不幸的是它接受文件路径字符串作为参数。

private WaveStream CreateInputStream(string fileName)
{
    WaveChannel32 inputStream;
    if (fileName.EndsWith(".mp3"))
    {
        WaveStream mp3Reader = new Mp3FileReader(fileName);
        inputStream = new WaveChannel32(mp3Reader);
    }
    else
    {
        throw new InvalidOperationException("Unsupported extension");
    }
    volumeStream = inputStream;
    return volumeStream;
}

To play the file: 要播放文件:

waveOutDevice = new WaveOut();
mainOutputStream = CreateInputStream("C:\\song.mp3");

Works fine with normal files, how would I go about files that are located in the Resources? 与普通文件一起正常工作,我将如何处理位于参考资料中的文件? Thank you. 谢谢。

The Mp3FileReader class can be constructed from either a filename or a System.IO.Stream . Mp3FileReader类可以从文件名或System.IO.Stream构造。 So what you need is to read the MP3 resource as a stream. 所以你需要的是将MP3资源作为流读取。 How you do that depends on how you've added the resource. 如何执行此操作取决于您添加资源的方式。

Resources added using the Properties/Resources.resx file (managed through the application properties dialog) are accessible through the Properties.Resources object. 可以通过Properties.Resources对象访问使用Properties/Resources.resx文件添加的Properties/Resources.resx (通过应用程序属性对话框管理)。 Known resource types (images, etc) should be represented here by their appropriate types, but MP3 files are accessed as byte[] . 已知的资源类型(图像等)应该由它们适当的类型表示,但MP3文件作为byte[]来访问。 You can create a MemoryStream from the resource and use that to construct the Mp3FileReader like so: 您可以从资源创建一个MemoryStream ,并使用它来构造Mp3FileReader如下所示:

MemoryStream mp3file = new MemoryStream(Properties.Resources.MP3file);
Mp3FileReader mp3reader = new Mp3FileReader(mp3file);

Other resource methods differ in the details of how you get the stream, but apart from that are basically the same. 其他资源方法在获取流的细节方面有所不同,但除此之外基本相同。 If you add an MP3 file to your project with the Embedded Resource build action, you can use the following: 如果使用Embedded Resource构建操作将MP3文件添加到项目中,则可以使用以下命令:

public Stream GetResourceStream(string filename)
{
    Assembly asm = Assembly.GetExecutingAssembly();
    string resname = asm.GetName().Name + "." + filename;
    return asm.GetManifestResourceStream(resname);
}

...
Stream mp3file = GetResourceStream("some file.mp3");
Mp3FileReader mp3reader = new Mp3FileReader(mp3file);

WPF resources are different again, using the pack:... uri format and Application.GetResourceStream . WPF资源再次不同,使用pack:... uri格式和Application.GetResourceStream

In all cases you should, of course, dispose the Stream once you're done reading it. 在所有情况下,您应该在阅读完之后处理Stream。

使用http://media.io/将其转换为.wav,然后您需要做的就是

(new System.Media.SoundPlayer(ProjectName.Properties.Resources.wavfilename)).Play();

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

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