Volume Control Demo

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only




Problem Description

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 the slider. 

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. 

Running/Exploring the Program 

bulletDownload executable
bullet Download source   

Suggestions for Further Explorations

Implement tests for Get volume, Set volume, and Mute routines for other mixer audio devices as well as the master device.  

Created:  September 21, 2015

Modified: May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.