我试图创建一个表单页面,用户必须在播放单词的声音后正确识别法语单词,然后在英文文本框中输入正确的答案。 我已将几个声音文件保存为WAV,并希望在表单中使用它并将其放置,就像它是一个声音文件列表一样。 当表单加载时,我希望每个按钮都保存它自己的声音文件,但这样做也会按照随机顺序调用声音文件,一旦button1被分配了声音文件,例如"多少?&# 34;然后button2无法调用相同的声音文件,因此删除"多少?"从声音文件列表中。 这是我到目前为止的代码
Public Class Form1
Dim SoundList As New List(Of String)
Dim RandomNumber As New Random
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim getInfo As System.IO.FileInfo
getInfo = My.Computer.FileSystem.GetFileInfo("C:\Users\MassiveNoob\Documents\FrenchWords")
'calling the file which has the sounds of each word
For Each Str As String In getInfo
SoundList.Add(Str)
Next
End Sub
Sub RandomizeSound()
Randomize()
Dim RandomSoundIndex As Integer = RandomNumber.Next(0, SoundList.Count)
'randomizing files in the sound file
My.Computer.Audio.Play(RandomSoundIndex)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RandomizeSound()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
RandomizeSound()
End Sub
结束班
答案 0 :(得分:0)
你快到了。有很多方法可以做到这一点,你的问题实际上有点广泛。
您当前的代码存在一些小问题。使用My.Computer.FileSystem.GetFiles会更容易,它只返回文件名列表而不是所有文件信息。
My.Computer.FileSystem.GetFiles("d:\temp", FileIO.SearchOption.SearchTopLevelOnly, "*.wav")
因此。上面的代码返回一组字符串。要将此集合添加到
getInfo.AddRange(My.Computer.FileSystem.GetFiles("d:\temp", FileIO.SearchOption.SearchTopLevelOnly, "*.wav"))
另外,我添加了两个参数来指定只搜索名为的目录,并且仅将带有.wav扩展名的文件名添加到列表中
每次单击按钮时,不要选择随机声音,而是创建一个预定义的唯一随机数列表,如下所示
private Sub CreateRandomIndexesToSoundList(numberOfButtons As Integer)
UniqueRandomNumber.Clear()
Dim index As Integer
For i As Integer = 0 To numberOfButtons
Do
index = RandomNumber.Next(0, SoundList.Count - 1)
Loop While UniqueRandomNumber.Contains(index)
UniqueRandomNumber.Add(index)
Next
End Sub
然后当您单击按钮1时,只需选择第一个索引,对于按钮2,选择第二个索引,依此类推。这样,您可以轻松扩展按钮数量。
所以完整的代码看起来像这样
Public Class Form1
Dim SoundList As New List(Of String)
Dim RandomNumber As New Random
Dim UniqueRandomNumber As New List(Of Integer)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'get a list of all .wav files in the search directory and
'add the path and filename to SoundList
SoundList.Clear()
SoundList.AddRange(My.Computer.FileSystem.GetFiles("d:\temp", FileIO.SearchOption.SearchTopLevelOnly, "*.wav"))
CreateRandomIndexesToSoundList(2)
End Sub
'Generates a list of unique random numbers in the range of 0 to the numberOfButtons
private Sub CreateRandomIndexesToSoundList(numberOfButtons As Integer)
UniqueRandomNumber.Clear()
Dim index As Integer
For i As Integer = 0 To numberOfButtons
Do
index = RandomNumber.Next(0, SoundList.Count - 1)
Loop While UniqueRandomNumber.Contains(index)
UniqueRandomNumber.Add(index)
Next
End Sub
Private Sub Playsound(index As Integer)
My.Computer.Audio.Play(SoundList(UniqueRandomNumber(index)))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Playsound(0)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Playsound(1)
End Sub
End Class