为什么 SoundPlayer 播放时会间歇性挂起?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为网络摄像头正在更新 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()
orSoundPlayer.Load
which both block the current thread.Instead try
SoundPlayer.LoadAsync()
andSoundPlayer.Play()
which use a separate thread.