WPF音頻具體內(nèi)容介紹
WPF在處理圖形界面顯示方面,具有非常強大的優(yōu)勢。可以方便輕松的使開發(fā)人員實現(xiàn)各種基于圖形的處理。通過對WPF的深入解讀,我們知道,WPF定義了一個SoundPlayerAction類(繼承自TriggerAction),它用一種友好的方式封裝了SoundPlayer類。#t#
這樣做的好處是,可以在控件的EventTrigger中添加SoundPlayerAciton動作,進而可以播放音頻文件。
WPF音頻代碼示例:
- < Button Content=
"xirihanlin"> - < Button.Triggers>
- < EventTrigger
RoutedEvent="Button.Click"> - < SoundPlayerAction Source="BLOW.WAV"/>
- < /EventTrigger>
- < /Button.Triggers>
- < /Button>
有SoundPlayerAction類的一個好處是你不用為播放音頻文件而在后臺書寫代碼。但是,這樣的好處也會給你帶來限制,因為你根本無法控制SoundPlayerAction與SoundPlayer之間的交互。
當點擊Button時,會創(chuàng)建SoundPlayerAction對象,而SoundPlayerAction內(nèi)部構(gòu)建了一個SoundPlayer實例,并把SoundPlayerAction的Source屬性值傳給了SoundPlayer實例,并調(diào)用了SoundPlayer的Play,而事實上,由于音頻文件沒有提前加載,你將不能在點擊的同時就能聽見聲音。因此,使用WPF音頻SoundPlayerAction類的限制還包括無法提前加載文件和設(shè)置循環(huán)播放等。