WPF中的音频——(3)

         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类的限制还包括无法提前加载文件和设置循环播放等。

posted on 2009-03-10 22:51  xirihanlin  阅读(1853)  评论(0编辑  收藏  举报