CONTROL POUR CHANGER LE VOLUME PRINCIPAL

chbu1983 Messages postés 3 Date d'inscription lundi 6 février 2006 Statut Membre Dernière intervention 11 août 2006 - 11 août 2006 à 09:56
cs_Laurent1313 Messages postés 24 Date d'inscription jeudi 19 juin 2003 Statut Membre Dernière intervention 15 décembre 2008 - 25 mai 2008 à 22:27
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/36595-control-pour-changer-le-volume-principal

cs_Laurent1313 Messages postés 24 Date d'inscription jeudi 19 juin 2003 Statut Membre Dernière intervention 15 décembre 2008
25 mai 2008 à 22:27
Salut, Vista gère le volume d'une façon complètement différente donc il y a en effet peu de chance que ce programme fonctionne correctement.

Il me semble que Vista gère le volume par programme plutôt que globalement, mais je n'ai aucune idée de la façon de controler ça avec Delphi.
Bioupy Messages postés 1 Date d'inscription samedi 18 août 2007 Statut Membre Dernière intervention 20 août 2007
20 août 2007 à 11:22
Superbe utilitaire !!!
Merci d'avoir diffusé la source.
Cela fonctionne très bien sur Windows XP mais n'est malheureusement pas
pris en compte sur Win VISTA :(
Si vous avez une solution... N'hésitez pas a me contacter.
En attendant, Je cherche de mon coté
Kiss :D
chbu1983 Messages postés 3 Date d'inscription lundi 6 février 2006 Statut Membre Dernière intervention 11 août 2006
11 août 2006 à 09:56
merci laurent pour ton code il m'a été super utile, j'ai mis un peu de temps un comprendre et à isoler le minimum nécessaire pour gérer uniquement le MASTER VOLUME (et oui çà fait pas plus d'une semaine que je connais delphi :)) mon code est peut être pas super bon ya des truc pas très ergonomique dû à l'appli que je veux faire derrière, en tout cas si çà peut aider des novices comme moi, je laisse ici le code que j'ai modifié, encore une fois merci beaucoup Laurent ;)


unit volume;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
mmsystem, ExtCtrls, StdCtrls, ComCtrls, shellApi, IniFiles;

type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
volumeBar: TProgressBar;
procedure teditkeypressed(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
procedure change_volume(volume: string);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
masterVolumeID:DWORD;
implementation

{$R *.dfm}

// gets the ID of the wanted mixer
function GetMixerControlID(CompType: integer) : DWORD;
var mxl: MIXERLINE;
mxc: MIXERCONTROL ;
mxlc: MIXERLINECONTROLS ;
begin

mxl.cbStruct := sizeof(mxl);
mxl.dwComponentType := CompType;

Result := 0;

if (mixerGetLineInfo(0, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) =
MMSYSERR_NOERROR) then begin
mxlc.cbStruct := sizeof(mxlc);
mxlc.dwLineID := mxl.dwLineID;
mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls := mxl.cControls;
mxlc.cbmxctrl := sizeof(mxc);
mxlc.pamxctrl := @mxc;
if (mixerGetLineControls(0, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) =
MMSYSERR_NOERROR) then Result := mxc.dwControlID;
end;

end;

// gives the volume of the given componant
function GetVolume( nIDControl: DWORD) : DWORD;
var Volume : Array[0..1] Of TMixerControlDetails_Unsigned;
mxcd: TMixerControlDetails;

begin

mxcd.cbStruct := sizeof(mxcd);
mxcd.dwControlID := nIDControl;
mxcd.cChannels := 2;
mxcd.cMultipleItems := 0;
mxcd.cbDetails := sizeof(MIXERCONTROLDETAILS_UNSIGNED) * 2;
mxcd.paDetails := @Volume;
Result := 0;

try
mixerGetControlDetails(0, @mxcd, MIXER_GETCONTROLDETAILSF_VALUE);
Result := Volume[0].dwValue;
except

end;
end;


// Changes the volume of the given componant
function SetVolume(ID, Value: DWord): Boolean;
var Volume : Array[0..1] Of TMixerControlDetails_Unsigned;
mxcd: TMixerControlDetails;
begin
Volume[0].dwValue := Value;
Volume[1].dwValue := Value;
With mxcd Do
Begin
cbStruct := SizeOf(mxcd);
dwControlID := ID;
cChannels := 2;
cMultipleItems := 0;
cbDetails := SizeOf(TMixerControlDetails_Unsigned) * 2;
paDetails := @Volume;
Result := (mixerSetControlDetails(0, @mxcd,
MIXER_SETCONTROLDETAILSF_VALUE) = MMSYSERR_NOERROR);
End; {With}
End;



procedure TForm1.change_volume(volume: string);
var
volume_int:integer;
current_volume: DWord;
begin
current_volume:= round(GetVolume( GetMixerControlID(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS))/655); // before calculating we get the current volume so that we can keep it if we want to
edit1.Text:=''; // only for the demonstration reset the text in edit1
if trystrtoint(volume,volume_int) then // checks if the volume entered is an integer
else volume_int:=999999; // if not, puts 999999 as the value of the integer meant to represent the volume
if volume_int=999999 then // if volume was not an integer then
if volume='min' then
volume_int:=0
else if volume='max' then
volume_int:=100
else if volume='+' then
volume_int:=round(current_volume*1.1)
else if volume='-' then
volume_int:=round(current_volume*0.9)
else volume_int:=current_volume; // if volume is nothing expected, it remains as it was
SetVolume(GetMixerControlID(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS), volume_int*655); // set the volume of the component MASTER VOLUME to volume_int wich is between 0 and 100 multiplied by 655 because the component wants a value between 0 and 65535
volumebar.Position:=volume_int; //only for the demonstration fills the bar of the left with a value representing the volume
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
edit1.Text:=''; // removes the text 'edit1' from the Edit1 object
Memo1.text:=' SET MASTER VOLUME'+#13#10#13#10+'0-100 --> percentage of volume'+#13#10+'min --> set volume to the minimum'+#13#10+'max --> set volume to the maximum'+#13#10+'+ --> increase the volume by 10%'+#13#10+'- --> decrease the volume by 10%';
change_volume(trim(edit1.text)); // to run the procedure while launching the program so that the volume bar will show us the volume immediately
end;

procedure TForm1.teditkeypressed(Sender: TObject; var Key: Char);
begin

if key=#13 then change_volume(trim(edit1.text)); // change the volume when the user presses ENTER
end;

end.
Rejoignez-nous