如何:从 Windows 窗体播放资源中嵌入的声音
可以使用 SoundPlayer 类来播放嵌入资源中的声音。
示例
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
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();
}
编译代码
此示例需要:
导入 System.Media 命名空间。
在项目中包括声音文件作为嵌入资源。
将“<程序集名称>”替换为嵌入了声音文件的程序集的名称。 请不要包括“.dll”后缀。