0

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?

level42
  • 946
  • 2
  • 13
  • 33
  • 2
    Are you sure you set the ChosenSound variable when the button is clicked? – Hanlet Escaño Jul 11 '14 at 21:03
  • I don't want to set the variable when the button is clicked. I want the button to play the last sound file that was set when selected from the combobox, if that makes any sense. – level42 Jul 11 '14 at 23:15
  • 1
    That error is because the stream isn't probably in the correct position, simply meaning it may not be done with the last stream. I will post solution that is tried and tested. – Trevor Jul 12 '14 at 02:36

1 Answers1

2

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
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • This is awesome! I'll plug the code in and test it soon, thanks for this! – level42 Jul 12 '14 at 20:56
  • This works perfectly! and cleans up my code in the process too. Thank you! – level42 Jul 12 '14 at 21:19
  • So, kind of a weird thing happening, I'm saving the string "LastItem" to a .setting, but when I load the setting, and apply the value to the ComboBox at load, it plays the audio on startup. – level42 Jul 13 '14 at 14:56
  • Declare a variable, then on load set it to true and after all your operations set it to false. In your selected index changed event, check that boolean, if true don't play it... – Trevor Jul 13 '14 at 16:29
  • How do I set the LastFile variable without playing? As soon as it's set, it's detected as a CBO change. I'm trying to read the last selected file and have a text label associated to it, displayed as the CBO.text property.'FrmSettings.optionsCBOselectsound.Text = LastFile' – level42 Jul 13 '14 at 19:31
  • Derp ... I figured it out ... Thanks again. – level42 Jul 13 '14 at 22:23