为什么 SoundPlayer 播放时会间歇性挂起?

发布于 2024-10-05 02:12:15 字数 445 浏览 0 评论 0原文

我有一个 WPF 应用程序,在其中使用 SoundPlayer 来播放一些简短的声音,例如键盘点击声。有时,看似随机,声音会停止播放。当我离开页面时,声音将在一次尖叫声中同时播放。

我的问题是,是否有任何明显的原因可以解释为什么会发生这种情况?

我已经尝试了几种方法,但由于我无法始终如一地重现该问题,因此很难找到原因。这些声音在整个应用程序中使用,因此我将它们在 app.xaml.cs 中加载到应用程序范围的静态集合中。我调用 SoundPlayer.Load() 以确保它们立即加载到内存中。

就像我说的,这永远不会完全停止工作。回放似乎堆积起来,直到导航到另一个页面,它们同时播放。

另一件可能产生影响的事情是我在应用程序中显示网络摄像头源。网络摄像头源是使用 DirectShow.NET 库加载的。我不确定加载图表是否会对声音播放产生任何不利影响。

I have a WPF application in which I'm using SoundPlayer to play several short sounds such as keyboard clicks. Sometimes, seemingly at random, the sounds will stop playing. When I navigate away from the page the sounds will then play all at once in one screeching playback.

My question is, are there any obvious reasons as to why this would happen?

I've tried several things but because I can't consistently reproduce the issue it's hard to find the cause. The sounds are used throughout the application, so I load them in app.xaml.cs into an application scoped static collection. I call SoundPlayer.Load() to ensure they're loaded into memory straight away.

Like I said, this never stops working completely. The play backs seem to pile up until navigating to another page where they all play at once.

One other thing that may have an impact is that I am displaying a webcam feed in the application. The webcam feed is loaded using the DirectShow.NET library. I'm not sure if loading graphs can have any adverse effect on the playback of sound.

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

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

发布评论

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

评论(1

心在旅行 2024-10-12 02:12:15

我认为网络摄像头正在更新 UI 元素,这将导致 UI 线程非常繁忙,在这种情况下,您可能不想使用 SoundPlayer.PlaySync()SoundPlayer.Load 两者都会阻塞当前线程。

请尝试使用单独线程的 SoundPlayer.LoadAsync()SoundPlayer.Play()

I suppose the web cam is updating a UI element which will cause the UI thread to be pretty busy, in that case you probably do not want to use SoundPlayer.PlaySync() or SoundPlayer.Load which both block the current thread.

Instead try SoundPlayer.LoadAsync() and SoundPlayer.Play() which use a separate thread.

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