使用 NAudio 加载 .wav 后退出程序时出现错误

发布于 2024-11-15 11:47:23 字数 776 浏览 4 评论 0原文

前几天我刚刚发现 NAudio 并且一直在玩它。我编写了一个简单的 Windows 窗体程序,它有一个加载按钮(加载特定的 .wav 歌曲)和播放、暂停、停止、淡入声音的按钮(我自己使用计时器和逐渐增加的音量添加的功能),以及淡出声音。我还有一个轨迹栏控件来控制音量。我创建了一个包装类来放入所有 NAudio 内容,并添加额外的功能,例如声音开始或暂停时的淡入/淡出和事件通知。

好吧,说了这么多,效果很好。程序正确加载、播放、暂停、停止以及淡入和淡出。音量轨迹栏正确反映了正在播放的歌曲的音量级别。这很有效,但我确实有两个问题。

首先,在我单击“加载”按钮加载声音,然后执行我想要(或不)想要的所有播放后,当我退出程序时,我收到以下弹出错误消息(“断言失败”):“AcmStreamHeader AcmStreamHeader.Finalize() 处未调用 dispose”。这是“中止、重试、忽略”弹出窗口之一,但几秒钟后它消失并且项目终止。 (注意:加载功能创建 DirectSoundOut 并调用 CreateInputStream... 直接取自站点上的 NAudio 示例。它还为定义的事件设置事件处理程序,但这可能并不重要。)

在类析构函数中,我拨打了以下电话:

mainOutputStream.Close();
mainOutputStream.Dispose();
waveOutDevice.Dispose();

但我仍然收到错误。这是一个大问题,我将在另一个线程中问另一个问题。知道为什么会发生这种情况以及如何阻止它吗?

我在 Windows 7 32 位上运行 VS 10。

I just found NAudio the other day and I've been playing with it. I have written a simple windows form program that has a load button (loads a specific .wav song), and buttons to play, pause, stop, fade the sound in (my own added functionality using timers and a gradually increasing volume), and fade the sound out. I also have a trackbar control to handle the volume. I created a wrapper class to put all the NAudio stuff in, as well as to add-on extra functionality like fade-in/out and event notifications when a sound starts or is paused.

Okay, all said, that works fine. The program correctly loads, plays, pauses, stops, and fades in and out. The volume trackbar correctly reflects the volume level of the song being played. That much works, but I do have two problems.

First, after I click the "Load" button to load the sound in and then do all the playing I want to (or not), when I exit the program I get the following popup error message ("Assertion failed"): "AcmStreamHeader dispose was not called at AcmStreamHeader.Finalize()". This is one of those "Abort, Retry, Ignore" popups, but after a few seconds it disappears and the project terminates. (Note: The load functionality creates the DirectSoundOut and calls CreateInputStream... taken directly from the NAudio samples on the site. It also sets up event handlers for the defined events, but that probably doesn't matter.)

In the class destructor, I make the following calls:

mainOutputStream.Close();
mainOutputStream.Dispose();
waveOutDevice.Dispose();

But I still get the error. This is the big one, and I'll ask the other question in another thread. Any idea why this is is happening and how I can stop it?

I am running VS 10 on Windows 7 32-bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

粉红×色少女 2024-11-22 11:47:23

该错误最可能的原因(它实际上只是一个 Debug.Assert - 它不会发生在 NAudio 的发布版本中),是 mainOutputStream 被分配给多个 WaveStream 并且当您更改它时,您没有处理旧的一。当您退出应用程序时,垃圾收集器将运行并触发此 Debug.Assert。

其他几点:

  • 无需调用 mainOutputStream.Close 和 Dispose。只需致电其中一位即可。
  • 您的清理代码不应位于类析构函数(终结器?)中,而应位于 Form 的 Dispose 方法中、Closing 或 Closed 事件处理程序中。
  • 编辑:另一个可能的原因是您从 WaveStream 派生,并且在重写的 Dispose 方法中没有对属于类成员的任何 WaveStream 调用 Dispose。

The most likely reason for the error (it's actually just a Debug.Assert - it doesn't occur in release builds of NAudio), is mainOutputStream was assigned to more than one WaveStream and when you changed it, you didn't Dispose the old one. When you exit the app, the Garbage Collector runs and this Debug.Assert fires.

A couple of other points:

  • There is no need to call mainOutputStream.Close and Dispose. Just call one of them.
  • Your cleanup code should not be in a class destructor (finalizer?), but in a Dispose method, for a Form, in the Closing or Closed event handler.
  • Edit: Another possible reason is that you derived from WaveStream and in your overriden Dispose method didn't call Dispose on any WaveStreams that are members of class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文