Page 1 of 1
Enable Windows Sleep while playing sound?!
Posted: Sat Jan 17, 2026 10:33 pm
by escape75
Consider the following code that plays a sound with a timer, the sound is just a bit shorter than the timer ... almost a loop ...
This seems to prevent windows from going to sleep, and I was wondering if anyone would have a solution to this ..
Code: Select all
InitSound()
UseOGGSoundDecoder()
OpenWindow(1, 200, 200, 200, 200, "Test")
If LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg")
AddWindowTimer(1, 2, 3000)
PlaySound(0)
EndIf
Repeat
Define Event = WaitWindowEvent()
If Event = #PB_Event_Timer And EventTimer() = 2
PlaySound(0)
EndIf
Until Event = #PB_Event_CloseWindow
Re: Enable Windows Sleep while playing sound?!
Posted: Sun Jan 18, 2026 2:57 pm
by RASHAD
I am not sure about your aim
If you want to send Window to sleep ( Hyphenate ) after finishing the play
You will need restart after
Else
You can turn the monitor on or off
Press Escape to Quit
Code: Select all
InitSound()
UseOGGSoundDecoder()
OpenWindow(1, 200, 200, 200, 200, "Test")
If LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg")
SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
PlaySound(0,#PB_Sound_Loop )
EndIf
Repeat
Select WaitWindowEvent()
Case #WM_CHAR
If EventwParam() = 27
StopSound(0)
SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_MONITORPOWER, -1)
Quit = 1
EndIf
EndSelect
Until Quit = 1
Re: Enable Windows Sleep while playing sound?!
Posted: Sun Jan 18, 2026 10:36 pm
by escape75
Thanks for the tips ... I was more interested in having the program not prevent regular scheduled sleep states,
however I believe the issue is not with PB but my Realtek driver, when sound plays it keeps a request open:
powercfg -requests
SYSTEM:
[DRIVER] Realtek High Definition Audio
An audio stream is currently in use.
In this case I don't think there's much I can do as the system will never sleep

Re: Enable Windows Sleep while playing sound?!
Posted: Sun Jan 18, 2026 10:49 pm
by RASHAD
Hi
You can use SetWindowCallback(@WndProc() [, #Window [, Mode]])
Code: Select all
Procedure WndProc(hwnd, uMsg, wParam, lParam)
result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_SYSCOMMAND
Select wParam
Case #SC_SCREENSAVE ;$F140
ProcedureReturn 0
Case #SC_MONITORPOWER ;$F170
ProcedureReturn 0
EndSelect
EndSelect
ProcedureReturn result
EndProcedure