Windows PlaySound
Windows PlaySound
Hi guys,
I'm using the PlaySound routine from the windows api to play wav files, but is there a way to stop the sound currently being played? There's only one sound playing at a time, so is there even a StopAllSounds function I have access to? Thanks
I'm using the PlaySound routine from the windows api to play wav files, but is there a way to stop the sound currently being played? There's only one sound playing at a time, so is there even a StopAllSounds function I have access to? Thanks
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Code: Select all
enum FLAGS
_ASYNC = &h000001
_NODEFAULT = &h000002
_MEMORY = &h000004
_LOOP = &h000008
_NOSTOP = &h000010
_PURGE = &h000040
_APPLICATION = &h000080
_NOWAIT = &h002000
_ALIAS = &h010000
_FILENAME = &h020000
_RESOURCE = &h040000 or _MEMORY
_ALIAS_ID = &h100000 or _ALIAS
end enum
#define PLAYFLAG _NOWAIT or _ASYNC or _LOOP or _FILENAME
#define SNDPLAYFLAG _ASYNC or _LOOP or _FILENAME
Declare Function PlaySound alias "PlaySoundA" (Byval As any ptr,Byval hModule as any ptr=0,Byval flag As integer=PLAYFLAG) As Integer
Declare Function sndPlaySound alias "sndPlaySoundA" (Byval As any ptr,Byval flag As Uinteger=SNDPLAYFLAG) As Integer
#inclib "winmm"
dim as string path = Environ("windir") & "\media\"
dim as string file = path & "notify.wav"
print file
PlaySound(strptr(file))
'print sndPlaySound(strptr(file))
sleep 3000,1
PlaySound(0,_NOWAIT)
sleep
Per the current MSDN PlaySound page SND_PURGE is not supported, but according to my PSDK from 2003 it is supported and it works as expected under Windows 2000, so it may work with some later versions.
Great, thanks. Though I have a simpler version:
Will this work with Windows XP(32/64)? I only tested this on Win 7 64-bit.
Code: Select all
#include once "windows.bi"
#include once "win\mmsystem.bi"
'Play Sound
PlaySound("Sound.wav", NULL, SND_ASYNC Or SND_FILENAME)
'Stop Sound
PlaySound(0, NULL, &h002000)
Will this work under Windows XP or later?
It works under Windows 2000, but there is a delay before the ("ir_inter.wav", null, SND_PURGE) call stops the sound. The (null, null, SND_PURGE) and (null, null, null) calls seem to stop the sound immediately.
Code: Select all
'==============================================================================
#define WIN_INCLUDEALL
#include "windows.bi"
'==============================================================================
print PlaySound( "ir_inter.wav", null, SND_FILENAME or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( "ir_inter.wav", null, SND_PURGE )
sleep
print PlaySound( "ir_inter.wav", null, SND_FILENAME or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( null, null, SND_PURGE )
sleep
print PlaySound( "ir_inter.wav", null, SND_FILENAME or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( null, null, null )
sleep
If the delay is caused by the file system, it may be possible to get around it by pre-loading the sound file (or resource) into a memory buffer and playing it from there.
Code: Select all
'==============================================================================
#define WIN_INCLUDEALL
#include "windows.bi"
'==============================================================================
'--------------------------------------------------------------
'' This assumes that ir_inter.wav is in the current directory.
'--------------------------------------------------------------
open "ir_inter.wav" for binary as 1
dim as ubyte ptr p = allocate(lof(1))
print Get(#1,,p[0],lof(1))
close
sleep
print PlaySound( p, null, SND_MEMORY or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( null, null, null )
sleep
-
- Posts: 87
- Joined: May 21, 2010 17:38
- Location: Massachusetts, USA
- Contact:
Michael, thanks for that, you're a code wizard. Now if I need to have multiple sounds pre-loaded, I assume I can do this:
And I presume you want to deallocate each sound after?
EDIT: I tested this code, and it only plays in looped mode, but slowing the system dramatically. I removed the loop flag, and now it doesn't work at all, it just freezes the program. Any ideas?
Code: Select all
#define WIN_INCLUDEALL
#include "windows.bi"
open "Sound1.wav" for binary as 1
dim as ubyte ptr SoundPtr_1 = allocate(lof(1))
print Get(#1,,SoundPtr_1[0],lof(1))
close
open "Sound2.wav" for binary as 1
dim as ubyte ptr SoundPtr_2 = allocate(lof(1))
print Get(#1,,SoundPtr_2[0],lof(1))
close
sleep
print PlaySound( SoundPtr_1, null, SND_MEMORY or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( SoundPtr_2, null, SND_MEMORY or SND_LOOP or SND_ASYNC )
sleep
print PlaySound( null, null, null )
sleep
EDIT: I tested this code, and it only plays in looped mode, but slowing the system dramatically. I removed the loop flag, and now it doesn't work at all, it just freezes the program. Any ideas?
In order to play few sounds in same time see this post :
http://www.freebasic.net/forum/viewtopi ... =playsound
http://www.freebasic.net/forum/viewtopi ... =playsound