Windows PlaySound

Windows specific questions.
Post Reply
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Windows PlaySound

Post by Ophelius »

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
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

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

MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

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.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Post by Ophelius »

Great, thanks. Though I have a simpler version:

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 with Windows XP(32/64)? I only tested this on Win 7 64-bit.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Works on Windows XP 32bit
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Post by Ophelius »

Cool, Thanks
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

Will this work under Windows XP or later?

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
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.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Post by Ophelius »

Speaking of delays, I got to try it on Win Xp and occasionally the sound didn't play until a few seconds after it was supposed to. But most of the time it did play on queue. Anybody know why that may happen, and what the flags actually mean?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

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
djsfantasi
Posts: 87
Joined: May 21, 2010 17:38
Location: Massachusetts, USA
Contact:

Post by djsfantasi »

Thanks for the tip, Michael.

I am going to try that this weekend.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Post by Ophelius »

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:

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
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?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

I think PlaySound does not support overlapping sounds from more than one source, within a given app. You can start multiple apps, and overlap the sound sources that way.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Post by SARG »

In order to play few sounds in same time see this post :

http://www.freebasic.net/forum/viewtopi ... =playsound
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Post by TJF »

@Site admin:

Can we move this to the windows topics?
Post Reply