I want to play random sounds, one after another. I can get one sound to play, but nothing more. It should play 10 random sounds from the list of 6. It just plays 1. How do I get it to play all 10?
`Private Sub RandomNote()

    For Me.a = 1 To 10
        Ran = New Random
        Play = Ran.Next(Min, Max)

        Select Case Play
            Case 1
                Play1()
            Case 2
                Play2()
            Case 3
                Play3()
            Case 4
                Play4()
            Case 5
                Play5()
            Case 6
                Play6()
        End Select

    Next a
End Sub`

Recommended Answers

All 6 Replies

First of all, it's hard to say what you are doing wrong unless we can see the code in the Subs Play1 through Play6. If you post some code we can have a look. In the meantime, I suggest you have one generic Sub called (for example) PlaySound. I also suggest you reference the sounds through an array (let's call it Sounds) so that you can do something like

For i As Integer = 1 to 10
    PlaySound(Sounds(Ran.Next(0,6)))
Next

Thanks for that - a neater idea.

The code I am using is simply:

Private Sub Play6()
        AxWindowsMediaPlayer1.URL = "c:\chords\6.mp3"
        AxWindowsMediaPlayer1.Ctlcontrols.play()
    End Sub

You have to wait for each sound to complete playing before starting the next. The following shows how to do that.

Private Sounds() As String = {
        "C:\Windows\Media\chimes.wav",
        "C:\Windows\Media\chord.wav",
        "C:\Windows\Media\ding.wav",
        "C:\Windows\Media\ir_begin.wav",
        "C:\Windows\Media\ir_end.wav",
        "C:\Windows\Media\ir_inter.wav",
        "C:\Windows\Media\notify.wav",
        "C:\Windows\Media\recycle.wav",
        "C:\Windows\Media\ringout.wav"}

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim rnd As New Random
    Dim file As String

    For i As Integer = 1 To 10
        file = Sounds(rnd.Next(0, UBound(Sounds) + 1))
        Me.Text = file
        My.Computer.Audio.Play(file, AudioPlayMode.WaitToComplete)
    Next

End Sub

Does that work for mp3 files too, or just WAV?

Just wav files. And not even all wav files.

For mp3 files you can go back to Windows Media Player but in order to play successive sounds/files you'll have to create a playlist. Here's an example. Note that the code uses three controls

  1. btnPlayFiles (button control)
  2. lbxStatus (ListBox control)
  3. axWmp ((Windows Media Player)

    Imports WMPLib
    
    Public Class Form1
    
        Private Sounds() As String = {
        "D:\My\Music\Billy Joel\Billy Joel - 52nd Street.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - A Matter of Trust.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Ain't No Crime.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - All About Soul.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Allentown.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - An Innocent Man.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - And So It Goes.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Baby Grand.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Big Shot.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Everybody Has a Dream.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Goodnight Saigon.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Half a Mile Away.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Hey Girl.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - Honesty.mp3",
        "D:\My\Music\Billy Joel\Billy Joel - I Go to Extremes.mp3"}
    
        Private Sub btnPlayFiles_Click(sender As Object, e As EventArgs) Handles btnPlayFiles.Click
    
            Dim rnd As New Random
            Dim file As String
    
            'Create a playlist of ten randomly selected files
            'Note that this does not guarantee ten UNIQUE randoom files
    
            Dim playList As WMPLib.IWMPPlaylist = axWmp.playlistCollection.newPlaylist("soundsToPlay")
    
            For i As Integer = 1 To 10
                file = Sounds(rnd.Next(0, UBound(Sounds) + 1))
                playList.appendItem(axWmp.newMedia(file))
            Next
    
            'Start playing the playlist
    
            axWmp.currentPlaylist = playList
            axWmp.Ctlcontrols.play()
    
        End Sub
    
        Private Sub axWmp_PlayStateChange(sender As Object, e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles axWmp.PlayStateChange
    
            lbxStatus.Items.Add(axWmp.status)
    
        End Sub
    
    End Class
    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.