Prev: WM_CLICK
Next: MMControl
From: Rencie on


Yes, please. With regards to your sndPlaySound (since I also tried it), I
don't know how to make it stop while it's playing. How do I make it stop
while it's currently playing?
From: MikeD on

"Rencie" <Rencie(a)discussions.microsoft.com> wrote in message
news:06B85D60-4556-43E2-BC74-ED5EA4C6C432(a)microsoft.com...
>
>
> Yes, please. With regards to your sndPlaySound (since I also tried it), I
> don't know how to make it stop while it's playing. How do I make it stop
> while it's currently playing?

I take it you're asking this regarding .wav files. As stated, sndPlaySound
(and, IMO, you really should be using PlaySound rather than sndPlaySound)
will only play .wav files. Since you've been focusing on midi files, I can
only assume you want to know this for future purposes when you might be
using .wav files.

To stop a sound that was started IN YOUR APP with PlaySound (or
sndPlaySound), that sound must have been started asynchronously. That means
the functions returns immediately after playback begins. By default,
PlaySound does not return until playback has finished. To play the sound
asynchronously, you must specify the SND_ASYNC flag when you call the
function. To stop that sound, and you can only stop that same sound, call
PlaySound again, but pass the intrinsic constant vbNullString for the
lpszName parameter.

Here's an example:

-----BEGIN CODE
Option Explicit

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal
lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME = &H20000 ' name is a file name
Private Const SND_ASYNC = &H1 ' play asynchronously
Private Const SND_LOOP = &H8 ' loop the sound until next
sndPlaySound
Private Const SND_NODEFAULT = &H2 ' silence not default, if sound
not found

Private Sub Form_Click()

Call PlaySound(vbNullString, 0&, SND_ASYNC)

End Sub

Private Sub Form_Load()

Dim sFilename As String

sFilename = "H:\WinXP\media\ding.wav"

Call PlaySound(sFilename, 0&, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT
Or SND_LOOP)

End Sub
----END CODE

In the above code, I hard-coded MY Windows directory. NEVER do this in your
actual code because you can't assume where Windows is installed on any given
system. Change the code above to use whatever folder you've installed
Windows to and remember it's just to TEST this example code. In production,
you need to use code like I posted earlier in this thread to determine at
runtime what the Windows directory actually is.

What this code does is loop playback of Windows' ding.wav file immediately
upon the form loading. The "ding" sound will continue to loop until you
click the form and then it will stop.



From: Martin Walke on
Rencie,

Here's some code to use the MMControl and make it loop. To be honest, we
stopped using the MMControl long time ago in favour of the ActiveMovie
control, due to performance issues.

Private Sub Form_Load()

With MMC1
.Visible = False
.UpdateInterval = 500 ' check position every 500ms (i.e. call
StatusUpdate)
.TimeFormat = 0
.FileName = "C:\WINDOWS\Media\flourish.mid"
.Notify = False ' don't use callbacks (Done event)
.Wait = True ' wait for each command to finish before moving on
.Shareable = False
' Open the MCI device.
.Command = "Open"
.Command = "play"
End With
End Sub

Private Sub MMC1_StatusUpdate()

With MMC1
If .Position >= .Length - 5 Then
.Command = "stop"
.Command = "seek"
.Command = "play"
End If
End With
End Sub

The -5 is there 'just in case' the MCI control fails to report the exact
position correctly (which can happen).

Mike mentioned the PlayCompleted event. That's only activated when using the
buttons on the actual controls.

One of the problems with MCI is the time delay between issueing commands and
activation. That's one of the reasons why we moved to AMovie.ocx. It's still
not the best but for most situations it'll do. :)

HTH
Martin


"Rencie" <Rencie(a)discussions.microsoft.com> wrote in message
news:06B85D60-4556-43E2-BC74-ED5EA4C6C432(a)microsoft.com...
>
>
> Yes, please. With regards to your sndPlaySound (since I also tried it), I
> don't know how to make it stop while it's playing. How do I make it stop
> while it's currently playing?


First  |  Prev  | 
Pages: 1 2 3 4
Prev: WM_CLICK
Next: MMControl