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
            Case 2
            Case 3
            Case 4
            Case 5
            Case 6
        End Select

    Next a
End Sub`
1 Year
Discussion Span
Last Post by Reverend Jim

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

Edited by Reverend Jim


Thanks for that - a neater idea.

The code I am using is simply:

Private Sub Play6()
        AxWindowsMediaPlayer1.URL = "c:\chords\6.mp3"
    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 = {

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)

End Sub

Edited by Reverend Jim


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))
            'Start playing the playlist
            axWmp.currentPlaylist = playList
        End Sub
        Private Sub axWmp_PlayStateChange(sender As Object, e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles axWmp.PlayStateChange
        End Sub
    End Class

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.