开发者

Why does SoundPlayer intermittently hang on playback?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜