Set the Duration of Audio File to Play (1 Viewer)

luzmen

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 6, 2017
Messages
10
Hello AccessWorld,


I have this Function code to play audio file in my Form:
Code:
Declare Function apisndPlaySound Lib "winmm" Alias "sndPlaySoundA" (ByVal filename As String, ByVal snd_async As Long) As Long

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

End Function

I do this code when the CountDown form is open:
Code:
Playsound ("C:\Users\mpc\Music\Downloaded Music\mycount1.wav")

However, when the countdown is over, the sound is still playing... Can i set the play duration according to where i want?

Hoping this is possible, i've searched a lot but not lucky enough to find one...
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:09
Joined
Feb 28, 2001
Messages
27,001
If the sound is a recorded .WAV, I think it has to play itself out unless you get into Task Manager to stop the audio playback. From VBA in Access, I doubt you have that ability.

If you were just playing a tone, not a .WAV, then you might have had a shot. But I didn't find anything either.

The code snippets you showed us came from Allen Browne's site: http://allenbrowne.com/func-04.html

If Allen didn't show a way to truncate the play duration, then either he didn't know how or he knew there wasn't a way. (Personally, I'm betting on the second case...)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:09
Joined
May 7, 2009
Messages
19,169
i think you can stop playing by calling the function again with an empty string:

PlaySound ""
 

isladogs

MVP / VIP
Local time
Today, 06:09
Joined
Jan 14, 2017
Messages
18,186
I use the following code to start / stop a wave file.
Copy it all to a standard module e.g. modSounds

Code:
Option Compare Database
Option Explicit

Public Const SND_ALIAS& = &H10000
Public Const SND_ASYNC& = &H1
Public Const SND_FILENAME& = &H20000
Public Const SND_NODEFAULT& = &H2
Public Const SND_NOWAIT& = &H2000
Public Const SND_RESOURCE& = &H40004
Public Const SND_SYNC& = &H0
Public Const SND_LOOP = &H8
Public Const SND_PURGE = &H40
Private Declare Function PlaySound& Lib "winmm.dll" Alias _
        "PlaySoundA" (ByVal lpszName As String, _
         ByVal hModule As Long, _
         ByVal dwFlags As Long)
         
Sub PlayWaveFile()
   PlaySound strSound, 0, SND_LOOP + SND_ASYNC
End Sub

Sub StopWaveFile()
   PlaySound vbNullString, 0, SND_PURGE
End Sub

In your form, select the sound using code similar to this:
Code:
strSound = CurrentProject.Path & "\Sounds\" & cboSound

I wrote this about 6 years ago so my memory is hazy. I think the SND_ASYNC part was essential for this to work

I used this code as part of my countdown timer in this thread: https://www.access-programmers.co.uk/forums/showthread.php?t=293308
Attached is an updated version which fixes a path error I've just discovered
 

Attachments

  • CountdownTimer1812.zip
    750.5 KB · Views: 380
Last edited:

luzmen

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 6, 2017
Messages
10
Thank you guys...

@The_Doc_Man .. thank you so much for the reply, however, there is a solution...
@arnelgp ... thanks, i tried your suggestion but it did not work..
@isladogs... YOUR CODE WORKS!! i only used the sub for stop playing.. and thanks for you attachment, i can use it in the future to improve my countdown...


Once Again Thank you so much!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:09
Joined
Feb 28, 2001
Messages
27,001
You are welcome. it often happens on this forum that if you search for the wrong thing you get the wrong answer - like I did. But fortunately, Colin had some experience that I didn't have. That's the power of the forum.
 

Users who are viewing this thread

Top Bottom