如何在 VB 中根据文本框的内容播放一系列音频文件?



我正在开发一个铁路公告系统,希望能够从列表中获取一些信息,例如"约克,达灵顿,达勒姆",然后在按下按钮后播放相关的录音。 谁能帮忙?

一种选择是创建一个字典,其中键表示列表项,值表示按下按钮后要播放的声音的文件位置。然后,将字典绑定到控件,其中控件的显示文本是字典的键,控件的值是字典的值。这样,您只需获取要播放的控件的选定值即可。

下面是一个使用 ComboBox 的示例:

Private stations As Dictionary(Of String, String)
Sub New()
stations = New Dictionary(Of String, String)
stations.Add("York, Darlington, Durham", "sound-a.mp3")
'stations.Add("...", "...")
With ComboBoxStations
.DataSource = New BindingSource(stations, Nothing)
.DisplayMember = "Key",
.ValueMember = "Value"
End With
End Sub
Private Sub PlaySound(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonPlaySound.Click
If (ComboBoxStations.SelectedValue IsNot Nothing) Then
My.Computer.Audio.Play(ComboBoxStations.SelectedValue.ToString(), AudioPlayMode.WaitToComplete)
End If
End Sub

最新更新