从MemoryStream打开音频(wav)文件以确定持续时间 [英] Opening an audio (wav) file from a MemoryStream to determine the duration

查看:141
本文介绍了从MemoryStream打开音频(wav)文件以确定持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是在框架内还是通过使用P/Invoke确定存储在MemoryStream中的wav文件的持续时间的方法?

Is there a way, either within the Framework or by using P/Invoke to determine the duration of a wav file that's held in a MemoryStream?

我已经看过托管DirectX 和另一个相似问题,但一切似乎都与路径兼容,而不是提供任何在流中传递的方式.我引用的问题中的链接之一(一个简单的C#Wave编辑器 ....)非常清楚地表明,我可以解析 MemoryStream来确定wav文件的持续时间.理想情况下,我不想重新发明轮子.

I've already had a look at Managed DirectX and another similar question, but everything seems to work with paths, rather than providing any way to pass in a stream. One of the links in the question I've referenced (A simple C# Wave editor....) makes it fairly clear that I could parse the MemoryStream to determine the duration of the wav file. Ideally I'd like to not re-invent the wheel.

推荐答案

我同意Alex的观点. 我花了一些时间编写一个包含三行代码的小程序,该程序可以打印wav文件的持续时间.

I agree with Alex. I took the time to put together a small program with three lines of code that prints the duration of a wav file.

        var stream=new MemoryStream(File.ReadAllBytes("test.wav"));
        var wave = new WaveFileReader(stream);
        Console.WriteLine(wave.TotalTime); // wave.TotalTime -> TimeSpan

下载 NAudio 库:您会在软件包中找到NAudio.dll.

Download NAudio library: you will find NAudio.dll in the package.

仅在您的项目中引用NAudio.dll.

Just reference NAudio.dll in your project.

在撰写本文时,它是1.3版.

At time of writing it's release 1.3.

就像作者在博客上说的那样,WaveFileReader也接受 Stream ;不只是文件路径.

Like the author says on his blog, WaveFileReader accept a Stream too; not just a file path.

请记住,版本1.3是为x86构建的.如果希望它在x64上运行,则需要将项目强制为x86. 如果您想要用于x64的NAudio.dll(像我一样),则需要使用"any cpu"重新编译. 对我来说,这两种解决方案都像魅力一样.

Remember that version 1.3 is built for x86. If you want it to work on x64 you need to force your project to x86. If you want NAudio.dll for x64 (like me) you need to recompile with 'any cpu'. For me both solutions worked like a charm.

这篇关于从MemoryStream打开音频(wav)文件以确定持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆