共用方式為


HOW TO:播放 Windows Form 中的音效

更新:2007 年 11 月

這個範例會在執行階段時,在指定的路徑中播放音效。

範例

Sub PlaySimpleSound()
    My.Computer.Audio.Play("c:\Windows\Media\chimes.wav")
End Sub
private void playSimpleSound()
{
    SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\chimes.wav");
    simpleSound.Play();
}

編譯程式碼

這個範例需要:

  • 您將檔案名稱 "c:\Windows\Media\chimes.wav" 取代為有效的檔案名稱。

  • (C#) 對 System.Media 命名空間的參考。

穩固程式設計

檔案作業應該放在適當的結構化例外處理區塊之中。

下列情形可能會導致例外狀況:

安全性

不要根據檔案名稱來判斷檔案內容。例如,檔案 Form1.vb 可能不是 Visual Basic 原始程式檔。在應用程式中使用這些資料之前,請先驗證所有輸入值。

請參閱

工作

HOW TO:在 Windows Form 中非同步地載入音效

參考

My.Computer 物件

SoundPlayer

其他資源

SoundPlayer 類別