Play/stop a specific audio file from an Access form database

I want to be able to play/stop a specific audio file from an Access form database.  Ideally, I click on a button to play the file and when I release the button, it stops.  I was thinking of using the following library - but can only seem to play an audio file - to the end.

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

Any guidance would be greatly appreciated.
Andy BrownDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupCommented:
Try this...

The first function goes at the top of your form's code.  The others show you how to start, pause, resume and stop playing a file from command buttons.  You can use the mouse down/mouse up events of a single command button as well to play/stop.  Also, this is the bare minimum code needed for these functions, and uses a hardcoded file name (change that to suit your form).  The mciSendString API returns a long integer, which can be used for robust error handling...

Option Compare Database
Option Explicit
Dim sFile As String
Private Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias _
   "mciSendStringA" (ByVal lpstrCommand As String, ByVal _
   lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
   hwndCallback As Long) As Long

Open in new window

Private Sub cmdPlay_Click()
Dim i
sFile = "Kalimba.mp3"
mciSendString "play " & sFile, 0&, 0, 0

End Sub

Private Sub cmdPause_Click()
mciSendString "pause " & sFile, 0&, 0, 0
End Sub

Private Sub cmdResume_Click()
mciSendString "resume " & sFile, 0&, 0, 0
End Sub

Private Sub cmdStop_Click()
mciSendString "close " & sFile, 0&, 0, 0
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Andy,

 Below is all the dwFlag constants.   You should call the function first with SND_ASYNC, then call the function again with SND_PURGE.

Jim.

SND_ALIAS = &H10000
lpszName is a string identifying the name of the system event sound to play.
SND_ALIAS_ID = &H110000
lpszName is a string identifying the name of the predefined sound identifier to play.
SND_APPLICATION = &H80
lpszName is a string identifying the application-specific event association sound to play.
SND_ASYNC = &H1
Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
SND_FILENAME = &H20000
lpszName is a string identifying the filename of the .wav file to play.
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.
SND_MEMORY = &H4
lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
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.
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.
SND_NOWAIT = &H2000
If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
SND_PURGE = &H40
Stop playback of any waveform sound. lpszName must be an empty string.
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.
SND_SYNC = &H0
Play the sound synchronously -- do not return until the sound has finished playing.
0
Andy BrownDeveloperAuthor Commented:
Superb - thank you guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.