Naudio:如何播放MP3和WAV文件?

发布于 2024-12-23 03:58:15 字数 1005 浏览 5 评论 0原文

好吧,我知道这对某些人来说听起来是一个非常简单的问题,但我真的被困在这里了。事实上,我正在使用 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();

    }
 }

这对于在简单的控制台应用程序中进行测试非常有用。然而,如果您实际上正在构建一个严肃的应用程序,那么这没有用。例如,许多教程从未提及如何处理最关键的事情,例如:

  1. 处置资源以及​​何时执行
  2. 处理不同异常的最佳方法
  3. 暂停、停止、倒带甚至退出应用程序之前要做什么
  4. 其他我什至不知道存在的东西。 因为我正在经历这个过程,并且注意到我的应用程序抛出了太多异常,所以有人可以分享一下他用来处理这个问题的 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:

  1. Disposing resource and when to do it
  2. The best ways to handle different exceptions
  3. What to do before you pause, stop, rewind or even exit the application
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-12-30 03:58:15
  1. 要释放非托管资源,请调用 WaveStreams 的 Close 方法。 “何时执行”部分相当明显......您真的不知道什么时候是处置非托管资源的正确时间吗?当您不再使用它们时,请丢弃它们。
  2. 这个问题我无法回答。对不起。
  3. 要暂停,请调用 WaveOut 对象的 Pause 方法。要倒回,请调用 WaveStream 的 Seek 方法。要停止,不要调用 WaveOut 的 Stop 方法。您必须调用 Pause,然后调用 WaveStream 的 Seek 方法才能转到缓冲区的开头。
  4. 引发所有异常的最可能原因是您显示的大多数代码实际上是不必要的。播放 MP3 文件所需要做的就是:
WaveStream mainOutputStream = new Mp3FileReader(path_of_the_file_you_want_to_play);
WaveChannel32 volumeStream = new WaveChannel32(mainOutputStream);
    
WaveOutEvent player = new WaveOutEvent();
    
player.Init(volumeStream);
    
player.Play();

我个人更喜欢使用 WaveOutEvent 而不是 WaveOut,因为它不需要您使用 Windows 窗体或 WPF,使您能够将 NAudio 用于您想要制作的任何类型的应用程序使用 C#,甚至 XNA 游戏。此外,WaveOutEvent 具有非常“即发即忘”的可用性,它的构造函数甚至不需要回调。

所有这些用于更改缓冲区内容(例如位深度的采样率)的 WaveStream 只是要求 NAudio 抛出异常的方法。像这样使用时它们很少起作用。如果您想转换缓冲区的某些内容,则必须调用 WaveFormatConversionStream 的一些静态方法(至少它们的名称是不言自明的。)

  1. To Dispose the unmanaged resources, you call the Close method of the WaveStreams. The "when to do it" part is rather obvious... Do you really don't know when it is the right time to Dispose unmanaged resources? You Dispose them when you are not going to use them anymore.
  2. I can't answer this one. Sorry.
  3. To Pause, you call the Pause method of the WaveOut object. To rewind, you call the Seek method of the WaveStream. To Stop, don't call the Stop method of the WaveOut. You must call Pause and then call the Seek method of the WaveStream to go to the beginning of the buffer.
  4. The most probable cause of all the Exceptions being thrown is because most of the code you shown is actually unnecessary. All you should need to do to play a MP3 file is:
WaveStream mainOutputStream = new Mp3FileReader(path_of_the_file_you_want_to_play);
WaveChannel32 volumeStream = new WaveChannel32(mainOutputStream);
    
WaveOutEvent player = new WaveOutEvent();
    
player.Init(volumeStream);
    
player.Play();

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文