0

我有大型 WPF 应用程序,它也使用 MEF。我想在特定条件下触发音频警报并在用户指定的时间内重复它。音频文件可以是 .wav 或 .mp3。

我正在使用 SoundPlayer 播放音频。我不确定重复间隔使用哪个计时器。

我不想在播放音频时阻塞 UI 线程,也希望它是线程安全的。

提前致谢。

4

1 回答 1

0

Why not consider using Event Aggregation, which is an implementation of publisher subscriber pattern. Whenever your application encounters a condition where it wants to play audio, it would publish its intensions via the event aggregator.

There would be a listener who listens for these events and would play the correct audio based on the type of the event. If there are multiple requests, the listener may play them in parallel or in a sequence. You can implement the desired threading model within the listener and standard thread-sleep

This way, you get to keep all you audio configuration and logic tucked behind a single module. All the other modules will just ‘tell’ this module what they want to play (and optionally for how long)

On the other hand, unless your application is large enough the above approach would be an overkill

A good write-up on event aggregation http://codebetter.com/glennblock/2009/02/23/event-aggregation-with-mef-with-and-without-eventaggregator/

于 2013-02-14T05:16:48.103 回答