存储在变量.WAV文件 [英] Storing .WAV files in a Variable
问题描述
我有,我想我的用户能够从挑选3 .wav文件。
I've got 3 .wav files that I'd like my users to be able to pick from.
我也随后进入一个ComboBox,并且像这样选择的。
I have then entered into a ComboBox, and selected like so.
Public ChosenSound As Object
-
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem = "Beep" Then
ComboBox1.Text = "Beep"
ChosenSound = My.Resources.beeps
PlayBackgroundSoundResource()
End If
If ComboBox1.SelectedItem = "Chime" Then
ComboBox1.Text = "Chime"
ChosenSound = My.Resources.chime
PlayBackgroundSoundResource()
End If
If ComboBox1.SelectedItem = "Chirp" Then
ComboBox1.Text = "Chirp"
ChosenSound = My.Resources.chirp
PlayBackgroundSoundResource()
End If
End Sub
-
Sub PlayBackgroundSoundResource()
Try
My.Computer.Audio.Play(ChosenSound, AudioPlayMode.Background)
Catch ex1 As Exception
MessageBox.Show(ex1.Message)
Return
End Try
End Sub
通过ComboBox中选定时,每个声音播放完全没有问题,但一旦声音通过其他的方式演奏,即一个按钮preSS,我得到以下错误:
Each sound plays perfectly fine when selected through the ComboBox, but once the sound is played through other means, I.E an button press, I get the following error:
---------------------------
---------------------------
The wave header is corrupt.
---------------------------
OK
---------------------------
下面是code为按钮preSS:
Here is the code for the button press:
Private Sub optionsBTNtestsound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optionsBTNtestsound.Click
PlayBackgroundSoundResource()
End Sub
我这样做都是错的?为什么我的声音只能播放一次的组合框选择,而不是在以任何其他方式叫什么?
Am I doing this all wrong? Why can my sound only play once selected by the ComboBox and not when called in any other way?
推荐答案
正如我在上面我的评论说,流可能不会在一开始,所以这就是为什么你看到波头已损坏
为了解决这个问题,不依赖于 Audio.Play
和流可能仍然无法完成您的错误的原因。这需要一个流和播放模式,如果你选择的项目左,右,流不这样做,那么你尝试播放另一个文件时,流没有结束。
As I have said in my comment above, the stream may not be at the beginning and hence this is why you are seeing The wave header is corrupt
To fix this, don't rely on the Audio.Play
as the stream may still not be done and the reason for your error. That takes a stream and the playmode, if your selecting items left and right, the stream isn't done and then your trying to play another file when the stream isn't at the end.
这是尝试和放大器;测试
Private LastFile As String = String.Empty 'Holds the last selected item
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
PlayBackgroundSoundResource(ComboBox1.SelectedItem.ToString) 'Call your method
LastFile = ComboBox1.SelectedItem.ToString.Trim 'Set your variable to the last item
End Sub
Private Sub PlayBackgroundSoundResource(ByVal strItem As String)
Dim sPlayer As New System.Media.SoundPlayer 'Create new instance of the soundplayer
Select Case strItem.Trim
Case "Beep"
sPlayer.Stream = My.Resources.beeps
Case "Chime"
sPlayer.Stream = My.Resources.chime
Case "Chirp"
sPlayer.Stream = My.Resources.chirp
End Select
sPlayer.Play() 'Play the file
If sPlayer IsNot Nothing Then sPlayer = Nothing
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PlayBackgroundSoundResource(LastFile) 'Play the last file that was selected
End Sub
这篇关于存储在变量.WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!