Naudio:如何播放MP3和WAV文件?
好吧,我知道这对某些人来说听起来是一个非常简单的问题,但我真的被困在这里了。事实上,我正在使用 Naudio 构建音频播放器,并且我意识到在许多教程中人们始终展示帮助您入门的简单方法。然而,在我看来,他们总是忘记展示在实际应用程序中实际是如何完成事情的。例如,当使用 Naudio 播放音乐时,我会执行以下操作:
Void PlayAudioMusic(string FilePath)
{
using (var ms = File.OpenRead(FilePath))
using (var rdr = new Mp3FileReader(ms))
using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
using (var baStream = new BlockAlignReductionStream(wavStream))
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(baStream);
waveOut.Play();
}
}
这对于在简单的控制台应用程序中进行测试非常有用。然而,如果您实际上正在构建一个严肃的应用程序,那么这没有用。例如,许多教程从未提及如何处理最关键的事情,例如:
- 处置资源以及何时执行
- 处理不同异常的最佳方法
- 暂停、停止、倒带甚至退出应用程序之前要做什么
- 其他我什至不知道存在的东西。 因为我正在经历这个过程,并且注意到我的应用程序抛出了太多异常,所以有人可以分享一下他用来处理这个问题的 Naudio 的包装类吗?我相信这将解决我们中的一些人在尝试使用 Naudio 时遇到的许多麻烦。
谢谢。
Okay, I know this sounds like a very easy question to some but I am really stuck here. Indeed, I am building an audio player using Naudio and I have realized that in many tutorials people always show easy ways to get you started. However, in my opinion, they always forget to show how things are actually done in a real application. For example, when playing music with Naudio, I would do something like:
Void PlayAudioMusic(string FilePath)
{
using (var ms = File.OpenRead(FilePath))
using (var rdr = new Mp3FileReader(ms))
using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
using (var baStream = new BlockAlignReductionStream(wavStream))
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(baStream);
waveOut.Play();
}
}
This is great for testing in a simple console application. This however isn't useful if you're actually building a serious application. For example, what many tutorials never say is for example how to handle the most critical things such as:
- Disposing resource and when to do it
- The best ways to handle different exceptions
- What to do before you pause, stop, rewind or even exit the application
- Other stuffs I don't even know exist.
Since I am going through this process and have notice that my application has way too many exceptions thrown, can someone please share like a wrapper class around Naudio that he used to handle this. I am sure this will answer many of the trouble some of us have been going through when trying to use Naudio.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人更喜欢使用 WaveOutEvent 而不是 WaveOut,因为它不需要您使用 Windows 窗体或 WPF,使您能够将 NAudio 用于您想要制作的任何类型的应用程序使用 C#,甚至 XNA 游戏。此外,WaveOutEvent 具有非常“即发即忘”的可用性,它的构造函数甚至不需要回调。
所有这些用于更改缓冲区内容(例如位深度的采样率)的 WaveStream 只是要求 NAudio 抛出异常的方法。像这样使用时它们很少起作用。如果您想转换缓冲区的某些内容,则必须调用 WaveFormatConversionStream 的一些静态方法(至少它们的名称是不言自明的。)
I personally prefer to use WaveOutEvent instead of WaveOut because it does not require you to be using Windows Forms or WPF, enabling you to use NAudio for absolutely any kind of application you want make with C#, even XNA games. Also, WaveOutEvent has a very fire-and-forget usability, and it's constructor does not even ask for a callback.
All these WaveStreams meant for changing stuff about the buffer (such as Sample Rate of Bit Depth) are just ways of asking for NAudio to throw an exception. They rarely work when used like this. If you want to convert some stuff of the buffers, you have to call some Static methods of the WaveFormatConversionStream (their names are self-explanatory, at least.)