如何:通过 Windows 窗体播放资源中嵌入的声音
可以使用 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”后缀。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈