Solved

MM control too slow...

Posted on 2002-05-20
5
192 Views
Last Modified: 2008-01-09
I'm working on a program that currently has the Multimedia Control to make a half second "beep" sound.  there are so many things going on in the program at the time of execution, that it takes about 2 seconds to play the wav file...

I'm looking for a lighter control, a FREEWARE control or another different way of playing a WAV file that will solve the problem.

if you need more info, just say so...
0
Comment
Question by:Smokintbird
  • 4
5 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 7023251
Your problem may be that you have a slow third party sound system on your machine.

What happens when you use:

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

See:

http://216.26.161.91/vbapi/ref/p/playsound.html

0
 
LVL 17

Expert Comment

by:inthedark
ID: 7023272
Paste this into a form then run.

Click on the form to run.

You may need to edit the wav file name:

"C:\winnt\media\tada.WAV" to something on your system.

Option Explicit

'Zero or more of the following flags specifying what lpszName refers to and how to play the sound:
Const SND_ALIAS = &H10000 'lpszName is a string identifying the name of the system event sound to play.
Const SND_ALIAS_ID = &H110000 'lpszName is a string identifying the name of the predefined sound identifier to play.
Const SND_APPLICATION = &H80 'lpszName is a string identifying the application-specific event association sound to play.
Const SND_ASYNC = &H1 'Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
Const SND_FILENAME = &H20000 'lpszName is a string identifying the filename of the .wav file to play.
Const SND_LOOP = &H8 'Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified.
Const SND_MEMORY = &H4 'lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
Const SND_NODEFAULT = &H2 'If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success.
Const SND_NOSTOP = &H10 'If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead.
Const SND_NOWAIT = &H2000 'If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
Const SND_PURGE = &H40 'Stop playback of any waveform sound. lpszName must be an empty string.
Const SND_RESOURCE = &H4004 'lpszName is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle.
Const SND_SYNC = &H0 'Play the sound synchronously -- do not return until the sound has finished playing.

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _
    ByVal hModule As Long, ByVal dwFlags As Long) As Long

Private Sub Form_Click()
Dim retval, ti
ti = Timer
retval = PlaySound("C:\winnt\media\tada.WAV", 0, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT)
MsgBox "It took " + Format(ti - Timer, "0.000") + " seconds"

End Sub


Private Sub Form_Unload(Cancel As Integer)
Dim retval
retval = PlaySound("", 0, SND_PURGE Or SND_NODEFAULT)  ' stop playback
End Sub


0
 
LVL 17

Expert Comment

by:inthedark
ID: 7023274
You must call when your form/app quits:

retval = PlaySound("", 0, SND_PURGE Or SND_NODEFAULT)  ' stop playback

If you try to qut while a sound is playing you get a GP Fault.

0
 
LVL 17

Accepted Solution

by:
inthedark earned 300 total points
ID: 7023379
So how you play the sound depends on what processing you need to do.

If you want to continue processing this example won't work becuase it assumes you are going to stop for half a seconds and do nothing.

retval = PlaySound("C:\Sounds\scream.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT Or SND_LOOP)
Sleep 500  ' wait for .5 seconds while sound loops
retval = PlaySound("", 0, SND_PURGE Or SND_NODEFAULT)  ' stop playback

The other way is to fire the play sound and then keep checking the time and stop the sound after .5 seconds has past. But the problem here is that if you do an intensive thing your sound may run on.

The best and simplest thing is to crop the beep so it is just the length you need it. When you wan't the bleep:

retval = PlaySound("C:\Sounds\scream.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT)

This will play until end of sound.
0
 
LVL 1

Author Comment

by:Smokintbird
ID: 7030802
worth the points...   the API works great, once I figured it all out...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question