Storing .WAV files in a Variable

。_饼干妹妹 提交于 2019-12-25 02:26:52

问题


I've got 3 .wav files that I'd like my users to be able to pick from.

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

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   
---------------------------

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?


回答1:


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.

This is tried & tested

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


来源:https://stackoverflow.com/questions/24706470/storing-wav-files-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!