2013-07-15 55 views
1

好吧,我的问题很简单。我在Visual Studio的Visual Basic项目中有3个资源,都是音频文件。我希望我的应用程序播放用户在TextBox中输入的名称。Visual Basic资源

进一步解释一下: 我有资源“火”,“水”和“空气”。如果用户键入空气在一个文本框,然后我用

My.Computer.Audio.Play(My.Resources.Air, AudioPlayMode.Background) 

但我不想为每个可能的代码的副本,我只想把它做成一个变量被播放作为目的。谁能帮我? :■

+0

改为使用My.Resources.ResourceManager.GetObject()。它需要一个字符串。 –

回答

0

你或许应该把这个变成一个case语句

Dim ResourceFilePathPrefix As String 
    If System.Diagnostics.Debugger.IsAttached() Then 
     'In Debugging mode  
     ResourceFilePathPrefix = System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\resources\") 
    Else 
     'In Published mode  
     ResourceFilePathPrefix = Application.StartupPath & "\resources\" 
    End If 

    'you name your files Fire.wav, Water.wav or whatever you want. 
    playerStream = ResourceFilePathPrefix & textBox1.text + ".wav" 

    My.Computer.Audio.Play(playerStream, AudioPlayMode.Background) 

,或者你可以直接引用它:

Try 
    My.Computer.Audio.Play("C:\TEST\" & textBox1.text,AudioPlayMode.Background) 
Catch ex As Exception 
    MsgBox("The file does not exist." & ex.Message, MsgBoxStyle.Critical) 
End Try 

假设你的WAV文件是@ C:\测试:)

+0

感谢您的回答,但是如果我有200个音频文件需要播放,那就很难了。没有其他方法可以做到吗? – user2558325

+0

查看修改,我为wav添加了textBox1.text – ApolloSoftware

+0

您只需简单地将您想要的文件命名为Resources,然后在其末尾添加.wav即可。我认为这应该工作。 – ApolloSoftware