This project is read-only.

Prevent sleep while Spotify is playing music

Jan 5, 2016 at 9:20 PM
Edited Jan 5, 2016 at 9:25 PM
Spotify does not prevent Windows from sleeping while playing music. There has been a feature request up for perhaps five years, I don't think the Spotify developers will ever add that feature.

There's even a separate app that specifically only prevents sleep while Spotify is playing.

Could the feature perhaps be added to Toastify too so that we don't need more apps to make Spotify behave? :)

See the source for that app on GitHub.

Specifically this file seems to do the work

Or perhaps look into SetThreadExecutionState which should also work?
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint SetThreadExecutionState(EXECUTION_STATE esFlags);

// When spotify starts playing
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED);

// When playback is paused or spotify is not running
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
See MSDN for SetThreadExecutionState
Mar 1, 2016 at 8:37 AM