共用方式為


操作說明:播放內嵌在 Windows Forms 資源中的音效

您可以使用 SoundPlayer 類別,從內嵌資源播放音效。

範例

private void playSoundFromResource(object sender, EventArgs e)
{
    System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream s = a.GetManifestResourceStream("<AssemblyName>.chimes.wav");
    SoundPlayer player = new SoundPlayer(s);
    player.Play();
}
Private Sub playSoundFromResource(ByVal sender As Object, _
ByVal e As EventArgs)
    Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    Dim s As System.IO.Stream = a.GetManifestResourceStream("<AssemblyName>.chimes.wav")
    Dim player As SoundPlayer = New SoundPlayer(s)
    player.Play()
End Sub

編譯程式碼

這個範例需要:

匯入 System.Media 命名空間。

將音效檔包含為專案中的內嵌資源。

將 "<AssemblyName>" 取代為其中內嵌音效檔的組件名稱。 請不要包括 ".dll" 尾碼。

另請參閱