[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Here's a demo program which can change the master sound volume with code.
Background & Techniques
A viewer recently asked if was possible use Delphi code to change the master volume control normally set
by clicking the speaker icon in the lower right screen corner and then dragging
Notes for programmers
The key to controlling device volume with code is an an API (Application Programming Interface) unit which connects the internal sound control routines to program code. Microsoft typically provides the API definition in the for of one of those other languages, but we need it in Delphi. Fortunately thus has been done by others in various versions and I located a working version of one, MMDevAPI.pas, at this MS Forum page. The unit is included with the source code download below. The only routine required is a call to SetMasterVolumeLevelScalar procedure in an instance of the IAudioEndpointVolume interface record. The routine uses a passed value between 0.0 and 1.0 to set the default audio device volume to a level between 0 and its maximum value.
I used the OnChange event of TTrackbar control to call the PlayBtnClick method. PlayBtnClick Sets the volume based on the current slider position and calls MessageBeep to play a sound to provide feedback of the current level. A check of time since the last call is made to prevent playing more often than once per 1/2 second. This eliminates multiple calls while sliding. the volume control.
Suggestions for Further Explorations
Copyright © 2000-2018, Gary Darby All rights reserved.