473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 2199

"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jeff Thies | last post by:
I'd like to randomly sort an array. A good method?
3
by: Gaffer | last post by:
Hello Is there a way in which I can make certain parts of Html on my website random so that each viewer will see different material if they refresh the page or come back onto the website later?...
6
by: ashu | last post by:
can any one tell me that is there any way to get a randomize (genuine) character like integer. for integer, we use random function. & for character, we use ????
1
by: Ellen Manning | last post by:
I've got an A2K database with a report that generates any number of random medical record numbers. The user inputs how many numbers they want and report uses the Randomizer function found on "The...
2
by: Rich | last post by:
Here is what I am trying for randomizing 2 numbers in the same subroutine so that they are not equal to each other: Dim j As Integer, k As Integer j = New System.Random().Next(0, 10) k = New...
1
by: Badass Scotsman | last post by:
Hello, This code is supposed to generate a random string each run, however I have had it live on a few sites, and it seems to create repeat strings all over the place. ...
1
by: Samuel Shulman | last post by:
Any function to randomize strings? thank you, Samuel
6
by: mrtaka79 | last post by:
Okay, first of all, I'm a complete noob, so go easy on me. I have this code that works perfectly for me. The only thing I want to add is to randomize the pictures/links that show up. Can anyone...
5
by: gggram2000 | last post by:
Hi, I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg. I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.