WPF定义了一个SoundPlayerAction类(继承自TriggerAction),它用一种友好的方式封装了SoundPlayer类。这样做的好处是,可以在控件的EventTrigger中添加SoundPlayerAciton动作,进而可以播放音频文件。

例如:

    <Button Content="xirihanlin">

        <Button.Triggers>

            <EventTrigger RoutedEvent="Button.Click">

                <SoundPlayerAction Source="BLOW.WAV"/>

            </EventTrigger>

        </Button.Triggers>

    </Button>

SoundPlayerAction类的一个好处是你不用为播放音频文件而在后台书写代码。但是,这样的好处也会给你带来限制,因为你根本无法控制SoundPlayerActionSoundPlayer之间的交互。

         当点击Button时,会创建SoundPlayerAction对象,而SoundPlayerAction内部构建了一个SoundPlayer实例,并把SoundPlayerActionSource属性值传给了SoundPlayer实例,并调用了SoundPlayerPlay,而事实上,由于音频文件没有提前加载,你将不能在点击的同时就能听见声音。因此,使用SoundPlayerAction类的限制还包括无法提前加载文件和设置循环播放等。

相关文章:

  • 2021-10-16
  • 2021-09-28
  • 2021-09-28
  • 2021-05-16
  • 2021-12-15
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2022-01-17
  • 2021-08-26
  • 2021-08-31
  • 2021-05-27
  • 2021-06-12
  • 2021-07-04
相关资源
相似解决方案