Enable Windows Sleep while playing sound?!

Just starting out? Need help? Post your questions and find answers here.
escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Enable Windows Sleep while playing sound?!

Post 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

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5027
Joined: Sun Apr 12, 2009 6:27 am

Re: Enable Windows Sleep while playing sound?!

Post 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

Code: Select all

RunProgram("shutdown"," /h","")
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
Egypt my love
escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Re: Enable Windows Sleep while playing sound?!

Post 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 :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5027
Joined: Sun Apr 12, 2009 6:27 am

Re: Enable Windows Sleep while playing sound?!

Post 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
Egypt my love
Post Reply