"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