By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Is there a way to randomize or loop through a set of wav sounds--for fun?

P: n/a
Hi there,

Here's the situation--there is a text field in a form in which students
will key in data. On the keypress event, I'd like for different sounds
to be played for each character typed, either randomly or in a loop (I
have a number of small, cool, computer-like sounds). I can get one
sound to play for the keypress event, but don't know how to play
multiple sounds.

Here's the codes for playing one sound...
______________________________

Private Sub Text0_KeyPress(KeyAscii As Integer)

PlaySound ("C:\WINDOWS\Media\Sound, bleep.wav")
______________________________

In a module mdlPlaySound

' http://odyssey.apana.org.au/~abrowne/func-04.html

Function PlaySound(sWavFile As String)

If apisndPlaySound(sWavFile, 1) = 0 Then
MsgBox "The Sound Did Not Play!"
End If
End Function

Many thanks in advance,
Arnold

Feb 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Arnold" <ee*******@kc.rr.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi there,

Here's the situation--there is a text field in a form in which students
will key in data. On the keypress event, I'd like for different sounds
to be played for each character typed, either randomly or in a loop (I
have a number of small, cool, computer-like sounds). I can get one
sound to play for the keypress event, but don't know how to play
multiple sounds.

Here's the codes for playing one sound...
______________________________

Private Sub Text0_KeyPress(KeyAscii As Integer)

PlaySound ("C:\WINDOWS\Media\Sound, bleep.wav")
______________________________

In a module mdlPlaySound

' http://odyssey.apana.org.au/~abrowne/func-04.html

Function PlaySound(sWavFile As String)

If apisndPlaySound(sWavFile, 1) = 0 Then
MsgBox "The Sound Did Not Play!"
End If
End Function

Many thanks in advance,
Arnold



Doing this might be more involved than you thought, especially if you want
to randomize these files. You would also have to specify more exactly the
random selections that are made. In this example, I look at all the wav
files in a folder, put them into a randomized order and then get keep
getting the next one until I have played each file once. Then I go back to
the start of the list (without re-randomizing) and repeat the procedure.

All of this code should be put in your form's code module.
Option Compare Database
Option Explicit

Private Const SOUNDS_FOLDER As String = "C:\Program Files\Windows
NT\Pinball\"
Dim m_astrFiles() As String
Dim m_lngIndex As Long

Private Function PlayNextFile() As Boolean

On Error GoTo Err_Handler

Dim strPath As String
Dim lngReturn As Long

If m_lngIndex > UBound(m_astrFiles()) Then
m_lngIndex = 0
End If

strPath = SOUNDS_FOLDER & m_astrFiles(m_lngIndex)

m_lngIndex = m_lngIndex + 1

If PlaySound(strPath) > 0 Then
PlayNextFile = True
End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Private Function CreateFileArray() As Boolean

On Error GoTo Err_Handler

Dim strFileList As String
Dim astrTemp() As String
Dim lngFileCount As Long
Dim lngCount As Long
Dim strValue As String
Dim strFile As String
Dim lngIndex As Long

astrTemp = Split(strFileList, "|")

lngFileCount = UBound(astrTemp()) + 1

strFile = Dir(SOUNDS_FOLDER & "*.wav")

Do Until Len(strFile) = 0
lngFileCount = lngFileCount + 1
strFileList = "|" & strFile & strFileList
strFile = Dir()
Loop

If Len(strFileList) > 1 Then
strFileList = Mid$(strFileList, 2)
End If

astrTemp() = Split(strFileList, "|")

ReDim m_astrFiles(0 To lngFileCount - 1)

Randomize

For lngCount = 0 To lngFileCount - 1
strValue = Format((1000000 * Rnd), "000000") & "_" & _
Format(lngCount, "000000")
m_astrFiles(lngCount) = strValue
Next lngCount

Call QuickSortArray(m_astrFiles())

For lngCount = 0 To lngFileCount - 1
strValue = m_astrFiles(lngCount)
strValue = Mid$(strValue, 8)
lngIndex = CLng(strValue)
m_astrFiles(lngCount) = astrTemp(lngIndex)
Next lngCount

CreateFileArray = True

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Private Function QuickSortArray(VarArray As Variant, _
Optional lngFirst As Long = -1, _
Optional lngLast As Long = -1) As Variant

Dim lngLow As Long
Dim lngHigh As Long
Dim lngMiddle As Long
Dim varTempVal As Variant
Dim varTestVal As Variant

If lngFirst = -1 Then lngFirst = LBound(VarArray)
If lngLast = -1 Then lngLast = UBound(VarArray)

If lngFirst < lngLast Then
lngMiddle = (lngFirst + lngLast) / 2
varTestVal = VarArray(lngMiddle)
lngLow = lngFirst
lngHigh = lngLast
Do
Do While VarArray(lngLow) < varTestVal
lngLow = lngLow + 1
Loop
Do While VarArray(lngHigh) > varTestVal
lngHigh = lngHigh - 1
Loop
If (lngLow <= lngHigh) Then
varTempVal = VarArray(lngLow)
VarArray(lngLow) = VarArray(lngHigh)
VarArray(lngHigh) = varTempVal
lngLow = lngLow + 1
lngHigh = lngHigh - 1
End If
Loop While (lngLow <= lngHigh)
If lngFirst < lngHigh Then QuickSortArray VarArray, lngFirst,
lngHigh
If lngLow < lngLast Then QuickSortArray VarArray, lngLow, lngLast
End If

End Function

Private Sub Form_Open(Cancel As Integer)
If Not CreateFileArray() Then
MsgBox "Error creating file array", vbExclamation
Cancel = True
End If
End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)
If Not PlayNextFile() Then
Exit Sub
End If
End Sub

Feb 6 '06 #2

P: n/a
I have hundreds of wav and mp3 files that I use in my database.
Some of the audio responses are informative ("The start date must be
earlier than the end date"); some are for entertainment ("Hotel
California"); and some are just plain foolishness. Most of the
responses are hard wired to a particular function. The music is
selectable off of a list. The foolishness just has to be endured.
I also have a few dozen pictures: Some for the employee file and some
I use RANDOMILY with my built in Screen Saver. I have a table with all
the picture names and a field that holds a number from 1 to N. Then I
generate a random number once per minute to pull the next picture from
the table. CInt((100 * Rnd) + 1) should give you a number between 1
and 100. Remember to use Randomize.
None of the sounds or pictures are in the database but are simply
referred to with a folder path and name when needed. Once you have
that Windows Media Player in place, you can also use it to play videos,
which I use for instructional purposes (Like how I do I braze a joint)
Good luck,
Hank Reed

Feb 6 '06 #3

P: n/a
"Hank" <ha********@aol.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I have hundreds of wav and mp3 files that I use in my database.
Some of the audio responses are informative ("The start date must be
earlier than the end date"); some are for entertainment ("Hotel
California"); and some are just plain foolishness. Most of the
responses are hard wired to a particular function. The music is
selectable off of a list. The foolishness just has to be endured.
I also have a few dozen pictures: Some for the employee file and some
I use RANDOMILY with my built in Screen Saver. I have a table with all
the picture names and a field that holds a number from 1 to N. Then I
generate a random number once per minute to pull the next picture from
the table. CInt((100 * Rnd) + 1) should give you a number between 1
and 100. Remember to use Randomize.
None of the sounds or pictures are in the database but are simply
referred to with a folder path and name when needed. Once you have
that Windows Media Player in place, you can also use it to play videos,
which I use for instructional purposes (Like how I do I braze a joint)
Good luck,
Hank Reed

Of course if you had all the paths stored in a table, it would be much
easier. However it does have the down side that someone has got to put them
there in the first place and also have to keep the list synchronized with
what's stored on the machine. If files are deleted from the os, then your
table will not be updated. The solution I outlined, looks them up 'live' so
to speak (but currently only looks at in one folder).


Feb 6 '06 #4

P: n/a
Thanks Anthony and Hank,

It's been a few days since I logged on and to my surprise, there is an
enormous chunk of code here--thanks very much for responding. I
normally store my sounds in the C:\Windows\Media folder. I'll try to
incorporate the code in a few days.

I've found that having something as easy as simple sounds and pictures
in a database sparks interest in the special kids I teach. I just
learned how to use the Gif89.dll to insert animations in Access and am
exited about that as well.

Take care,
Arnold

Feb 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.