如何在 VB.NET 中从资源播放 .wav
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7116669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play .wav in VB.NET from resources
提问by user807461
I'm polishing a VB.NET hangman game. I added the sound of a turkey gobbling when you win the game. This is in my loop for when you win:
我正在打磨 VB.NET 刽子手游戏。当你赢得比赛时,我添加了火鸡狼吞虎咽的声音。当你获胜时,这是我的循环:
Dim sndPing As New SoundPlayer(My.Resources.turkey)
sndPing.Play()
My.Computer.Audio.Play("C:\Users\john\Desktop\CS120_FinalProject_Hannonv2.0\CS120_FinalProject_Hannonv1.5\FinalProject_Hangman_Hannon\FinalProject_Hangman_Hannon\Resources\turkey.wav", AudioPlayMode.Background)
I have loaded the turkey.wav file into my resources, but I cannot give it a local directory with the "\Resources\turkey.wav" or My.Resources.turkey. I'm trying to find a way to send the file when I package it.
我已将 turkey.wav 文件加载到我的资源中,但我无法为它提供带有“\Resources\turkey.wav”或 My.Resources.turkey 的本地目录。我试图找到一种在打包文件时发送文件的方法。
回答by E_Blue
Easy, if your resource audio file is called "Ding"
很简单,如果你的资源音频文件叫做“叮”
My.Computer.Audio.Play(My.Resources.Ding, AudioPlayMode.Background)
That's all folks! :)
这就是所有的人!:)
回答by feathj
Your code is doing the same thing twice. First you create a SoundPlayer object with an embedded resources, and then you are calling the static function "My.Computer.Audio.Play". You will want to do one or the other.
你的代码做了两次同样的事情。首先创建一个带有嵌入资源的 SoundPlayer 对象,然后调用静态函数“My.Computer.Audio.Play”。你会想做一个或另一个。
The advantage with the SoundPlayer is you can use an embedded resource (so you don't have to track the wav file down). But it does require a bit more setup.
SoundPlayer 的优点是您可以使用嵌入式资源(因此您不必跟踪 wav 文件)。但它确实需要更多的设置。
If you want to use the static function, you can pass in a path relative to your exe location. Something like this:
如果你想使用静态函数,你可以传入一个相对于你的 exe 位置的路径。像这样的东西:
My.Computer.Audio.Play(System.AppDomain.CurrentDomain.BaseDirectory & "\turkey.wav")
Note that your wav file will need to be in the same folder as your .exe for the above code to work.
请注意,您的 wav 文件需要与 .exe 位于同一文件夹中,才能使上述代码正常工作。