1. 程式人生 > >Control The Volume Level In Windows 10 On Lock And Unlock

Control The Volume Level In Windows 10 On Lock And Unlock

Control The Volume Level In Windows 10 On Lock And Unlock

I got tired of having to lower the volume level when I went outside of my room at night to avoid waking up other people in the house. This would happen even with headphones since they can be quite loud. So I decided to do something to automate volume decrease and increase on lock screen actions, since I always lock my screen when leaving my desk.

I first tried with Autohotkey, which can actually change volume level on certain key combinations like “LWin & L” but I couldn’t find a way to do a certain task if the screen was locked or not. So I kept asking around. (Though an Autohotkey solution is provided at the bottom which might be better for you).

After some googling and some help in the freenode ##windows channel I came up with a solution which I’m going to share right now.

This solution will decrease volume to 20% on lock screen, and restore it to 100% on unlock. Of course this can be modified.

This DLL will let us control audio devices with Powershell.

I would save it somewhere in your documents under a directory called “dll” or however you want to call it, just make sure it’s a directory you won’t delete accidentally later on.

Open Powershell and run the commands specified in the Github repo above, which I’m going to share here but might get outdated later on, so check they’re the same.

The only thing that varies in those commands is where it says “C:\Path\to\AudioDeviceCmdlets.dll” which should point to wherever you stored the DLL.

New-Item "$($profile | split-path)\Modules\AudioDeviceCmdlets" -Type directory -Force
Copy-Item "C:\Path\to\AudioDeviceCmdlets.dll" "$($profile | split-path)\Modules\AudioDeviceCmdlets\AudioDeviceCmdlets.dll"
Set-Location "$($profile | Split-Path)\Modules\AudioDeviceCmdlets"
Get-ChildItem | Unblock-File
Import-Module AudioDeviceCmdlets

If everything executed correctly then you’re done with that.

Now we need to create 2 Powershell script files, which will get executed on lock or unlock.

Open an editor and save this as lower_volume.ps1

Set-AudioDevice -PlaybackVolume 20

Then save another file as max_volume.ps1

Set-AudioDevice -PlaybackVolume 100

I would save this somewhere in your documents in a directory called “scripts”.

Then all we need to do is create some tasks in Window’s own “Task Scheduler”.

Open the main menu and type “Scheduler” to find and open “Task Scheduler”.

Look for “Action” in the menu bar on the top, then select “Create Task”.

On that window, go to the “Triggers” tab, then click “New”, then on “Begin the task” select “On workstation lock”. Click “Ok”.

Now go to the “Actions” tab, click “New”.

On that window select “Start a program” then on “Program/script” input:

powershell

And on “Add arguments” below add:

-ExecutionPolicy RemoteSigned -File C:\Users\you\Documents\scripts\lower_volume.ps1

The RemoteSigned flag is to allow Powershell to run local scripts, at least I couldn’t run them without this, so there’s a fair chance you need it, you can try running it without it if you want.

Click “Ok” and the task should be ready. Now it will lower volume on lock.

Now we only need to restore volume to 100% on unlock. Simply repeat the steps above to create another task but using the “On workstation unlock” trigger and using the “max_volume.ps1” script action.

And you’re done.

Note: This can be a problem if you’re playing music and your computer locks for some reason, causing the volume to decrease. In this case maybe you want to change the lower_volume script for an Autohotkey action.

Here’s an Autohotkey solution that decreases the volume and locks the computer on a custom command (Shift+Win+L):

+#L::
 SoundSet 20
 run rundll32.exe user32.dll LockWorkStation