注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

控制主音量的代码  

2008-11-25 14:20:19|  分类: Delphi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

mute the master volume of your soundcard?
uses
  MMSystem;

function GetMasterMute(
  Mixer: hMixerObj;
  var Control: TMixerControl): MMResult;
  // Returns True on success
var
  Line: TMixerLine;
  Controls: TMixerLineControls;
begin
  ZeroMemory(@Line, SizeOf(Line));
  Line.cbStruct := SizeOf(Line);
  Line.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
  Result := mixerGetLineInfo(Mixer, @Line,
    MIXER_GETLINEINFOF_COMPONENTTYPE);
  if Result = MMSYSERR_NOERROR then
  begin
    ZeroMemory(@Controls, SizeOf(Controls));
    Controls.cbStruct := SizeOf(Controls);
    Controls.dwLineID := Line.dwLineID;
    Controls.cControls := 1;
    Controls.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
    Controls.cbmxctrl := SizeOf(Control);
    Controls.pamxctrl := @Control;
    Result := mixerGetLineControls(Mixer, @Controls,
      MIXER_GETLINECONTROLSF_ONEBYTYPE);
  end;
end;

procedure SetMasterMuteValue(
  Mixer: hMixerObj;
  Value: Boolean);
var
  MasterMute: TMixerControl;
  Details: TMixerControlDetails;
  BoolDetails: TMixerControlDetailsBoolean;
  Code: MMResult;
begin
  Code := GetMasterMute(0, MasterMute);
  if Code = MMSYSERR_NOERROR then
  begin
    with Details do
    begin
      cbStruct := SizeOf(Details);
      dwControlID := MasterMute.dwControlID;
      cChannels := 1;
      cMultipleItems := 0;
      cbDetails := SizeOf(BoolDetails);
      paDetails := @BoolDetails;
    end;
    LongBool(BoolDetails.fValue) := Value;
    Code := mixerSetControlDetails(0, @Details,
MIXER_SETCONTROLDETAILSF_VALUE);
  end;
  if Code <> MMSYSERR_NOERROR then
    raise Exception.CreateFmt('SetMasterMuteValue failure, '+
      'multimedia system error #%d', [Code]);
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetMasterMuteValue(0, CheckBox1.Checked); // Mixer device #0 mute on/off
end;

----------
mixerSetControlDetails
auxoutsetvolume 是控制CD音量的 
waveOutSetVolume 是控制波形的 
midiOutSetVolume 是控制FM或者是synthesizer

unit Volume;

interface

uses windows,mmsystem;

type 
    Tvolume=record
       left,right:word; // 取值范围 0--65535
    end;

procedure fillstruct(control:PMixerControl;var Cdetails:tMIXERCONTROLDETAILS);
function getpeak(control:PMixerControl;var peak:integer):boolean;
function setvolume(control:Pmixercontrol; volume:Tvolume):boolean;
function getvolume(control:Pmixercontrol; volume:Tvolume):boolean;

var
    mcontrols:array of array of array of PMixerControl; //mixer的数组,单声卡可以不用
    fmixerhandle:HMIXER;   //mixer的句柄

implementation

procedure fillstruct(control:PMixerControl;var Cdetails:tMIXERCONTROLDETAILS);
begin
    Cdetails.cbStruct:=sizeof(cdetails);
    cdetails.dwControlID:=Control.dwControlID;
    cdetails.cbDetails:=sizeof(integer);
    cdetails.hwndOwner:=0;
end;

function getpeak(control:PMixerControl;var peak:integer):boolean;
var
   details:TMixerControlDetailsSigned;
   cdetails:tMIXERCONTROLDETAILS;
begin
    Result:=false;
    if control.dwControlType<> mixercontrol_controltype_peakmeter then exit;
    cdetails.cChannels:=1;
    cdetails.paDetails:=@details;
    fillstruct(control,cdetails);
    result:=mixerGetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
end;

function setvolume(control:Pmixercontrol; volume:Tvolume):boolean;
var
    details:array[0..30] of integer;
    cdetails:tMIXERCONTROLDETAILS;
begin
    fillstruct(control,cdetails);
    cdetails.cChannels:=2;
    cdetails.paDetails:=@details;
    details[0]:=volume.left;
    details[1]:=volume.right;
    result:=mixerSetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
    volume.left:=details[0];
    volume.right:=details[1];
end;

function getvolume(control:Pmixercontrol; volume:Tvolume):boolean;
var
    details:array[0..30] of integer;
    cdetails:tMIXERCONTROLDETAILS;
begin
    fillstruct(control,cdetails);
    cdetails.cChannels:=2;
    cdetails.paDetails:=@details;
    result:=mixerGetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
    volume.left:=details[0];
    volume.right:=details[1];
end;

end.

// 调用方法:
procedure TForm1.Button1Click(Sender: TObject);
var  s : Tvolume;
     kz : Pmixercontrol;
begin
    new(kz); //<---------- 此处一定要分配内存
kz.dwControlID := 0;
s.left := 0;
s.right := 0;
try
setvolume(kz,s);
except
end;
freemem(kz);
end;

--------------------------------------------------------------------------------

unit Unit2;

interface

uses MMSystem, Dialogs;

Type
TDeviceType = (dtMaster, dtMicrophone, dtWaveOut, dtSynth, dtCD);
//主音量,MIC,Wave输出,MIDI输出,CD输出.
TValueType = (vtVolume, vtMute, vtDeviceName);
TAUDIODevice = Class
private
FDeviceType:TDeviceType;
FDeviceName:String;
procedure SetMute(const Value: Boolean);
procedure SetVolume(const Value: Word);
function GetValue(ValueType:TValueType): Word;
function GetVolume: Word;
function GetMute: Boolean;
procedure SetValue(Value: Word; ValueType: TValueType);
function GetDeviceName: String;
Public
Property DeviceType:TDeviceType read FDeviceType write FDeviceType;
Property DeviceName:String Read GetDeviceName;
Property Volume:Word Read GetVolume Write SetVolume;
Property Mute:Boolean Read GetMute Write SetMute;
end;

const DeviceTypeName : Array [TDeviceType] of String = ('Master', 'Microphone', 'WaveOut', 'Synth','CD');

implementation
Uses sysutils;
{ TAUDIODevice }

//设置音量或者静音
procedure TAUDIODevice.SetValue(Value: Word; ValueType: TValueType);
var
PriMixer: HMIXER;
PriMixLineControl: MIXERLINECONTROLS;
PriMixerControlDetails: TMIXERCONTROLDETAILS;
PriMixerControlDetails_Unsigned: TMIXERCONTROLDETAILS_UNSIGNED;
PriMixerControl: MIXERCONTROL;
PriMixerLine: TMixerLine;
iReturn: Integer;
begin
if (mixerGetNumDevs() < 1) then
Exit;
if ValueType=vtDeviceName then
Exit;

iReturn := mixerOpen(@PriMixer, 0, 0, 0, 0);
if iReturn = MMSYSERR_NOERROR then
begin
case FDeviceType of
dtMaster : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
dtMicrophone : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
dtWaveOut : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
dtSynth : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
dtCD : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
end;
PriMixerLine.cbStruct := SizeOf(PriMixerLine);

iReturn := mixerGetLineInfo(PriMixer, @PriMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);

if iReturn = MMSYSERR_NOERROR then
begin
FillChar(PriMixLineControl, SizeOf(PriMixLineControl),0);
PriMixLineControl.cbStruct := SizeOf(PriMixLineControl);
PriMixLineControl.dwLineID := PriMixerLine.dwLineID;
if ValueType=vtMute then
PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE
else if ValueType=vtVolume then
PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
PriMixLineControl.cControls := 1;
PriMixLineControl.cbmxctrl := SizeOf(PriMixerControl);

PriMixLineControl.pamxctrl := @PriMixerControl;
iReturn := mixerGetLineControls(PriMixer, @PriMixLineControl, MIXER_GETLINECONTROLSF_ONEBYTYPE);

if iReturn = MMSYSERR_NOERROR then
begin
FillChar(PriMixerControlDetails, SizeOf(PriMixerControlDetails),0);
PriMixerControlDetails.dwControlID := PriMixerControl.dwControlID;
PriMixerControlDetails.cbStruct := SizeOf(PriMixerControlDetails);
PriMixerControlDetails.cMultipleItems := 0;
PriMixerControlDetails.cbDetails := SizeOf(PriMixerControlDetails_Unsigned);
PriMixerControlDetails.paDetails := @PriMixerControlDetails_Unsigned;
PriMixerControlDetails.cChannels := 1;

PriMixerControlDetails_Unsigned.dwValue := Value;

iReturn := mixerSetControlDetails(PriMixer, @PriMixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);

if iReturn <> MMSYSERR_NOERROR then
Raise Exception.Create('Set Control Details Error!');
end
else
Raise Exception.Create('Get Control Line Information Error!');
end;
mixerClose(PriMixer);
end;
end;

function TAUDIODevice.GetValue(ValueType:TValueType): Word;
var
PriMixer: HMIXER;
PriMixLineControl: MIXERLINECONTROLS;
PriMixerControlDetails: TMIXERCONTROLDETAILS;
PriMixerControlDetails_Unsigned: TMIXERCONTROLDETAILS_UNSIGNED;
PriMixerControl: MIXERCONTROL;
PriMixerLine: TMixerLine;
iReturn: Integer;
begin
Result:=0;
//检查是否有相关设备.
FDeviceName:='';
if (mixerGetNumDevs() < 1) then
Exit;

//打开Mixer设备.
iReturn := mixerOpen(@PriMixer, 0, 0, 0, 0);
if iReturn = MMSYSERR_NOERROR then
begin
//初始化参数.
case FDeviceType of
dtMaster : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
dtMicrophone : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
dtWaveOut : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
dtSynth : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
dtCD : PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
end;
PriMixerLine.cbStruct := SizeOf(PriMixerLine);

//得到设备信息.
iReturn := mixerGetLineInfo(PriMixer, @PriMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);

if iReturn = MMSYSERR_NOERROR then
//返回正常.
begin
if ValueType=vtDeviceName then
FDeviceName:=PriMixerLine.szName
else
begin
FillChar(PriMixLineControl, SizeOf(MIXERLINECONTROLS),0);
//清零.
PriMixLineControl.cbStruct := SizeOf(PriMixLineControl);
PriMixLineControl.dwLineID := PriMixerLine.dwLineID;
if ValueType=vtMute then
PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE
else if ValueType=vtVolume then
PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
PriMixLineControl.cControls := 1;
PriMixLineControl.cbmxctrl := SizeOf(MIXERCONTROL);

PriMixLineControl.pamxctrl := @PriMixerControl;
iReturn := mixerGetLineControls(PriMixer, @PriMixLineControl, MIXER_GETLINECONTROLSF_ONEBYTYPE);

if iReturn = MMSYSERR_NOERROR then
begin
FillChar(PriMixerControlDetails, SizeOf(PriMixerControlDetails),0);
PriMixerControlDetails.dwControlID := PriMixerControl.dwControlID;
PriMixerControlDetails.cbStruct := SizeOf(PriMixerControlDetails);
PriMixerControlDetails.cMultipleItems := 0;
PriMixerControlDetails.cbDetails := SizeOf(PriMixerControlDetails_Unsigned);
PriMixerControlDetails.paDetails := @PriMixerControlDetails_Unsigned;
PriMixerControlDetails.cChannels := 1;

//得到详细信息.
iReturn := mixerGetControlDetails(PriMixer, @PriMixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);

//音量.
Result := PriMixerControlDetails_Unsigned.dwValue;

if iReturn <> MMSYSERR_NOERROR then
Raise Exception.Create('Get Control Details Error!');
end
else
Raise Exception.Create('Get Control Line Information Error!');
end;
end;
mixerClose(PriMixer);
//关闭.
end;
end;

function TAUDIODevice.GetMute: Boolean;
begin
Result:=GetValue(vtMute)<>0;
end;

procedure TAUDIODevice.SetMute(const Value: Boolean);
begin
SetValue(ABS(ORD(Value)),vtMute);
end;

procedure TAUDIODevice.SetVolume(const Value: Word);
begin
SetValue(Value,vtVolume);
end;

function TAUDIODevice.GetVolume: Word;
begin
Result:=GetValue(vtVolume);
end;

function TAUDIODevice.GetDeviceName: String;
begin
GetValue(vtDeviceName);
Result:=FDeviceName;
end;

------------------------------------------------------------------------

在进行多媒体软件开发时,经常要调整各种设备的音量和设置静音,本人编写了一个单元,四个函数,分别用于获取音量(GetVolume(DN))、设置音量(SetVolume(DN,Value))、获取静音(GetVolumeMute(DN))及设置静音(SetVolumeMute(DN,Value))。

unit funVolume;

interface

uses MMSystem, Dialogs;

Type TDeviceName = (Master, Microphone, WaveOut, Synth);

function  GetVolume(DN:TDeviceName) : Word ;
procedure SetVolume(DN:TDeviceName; Value:Word);
function  GetVolumeMute(DN:TDeviceName) : Boolean;
procedure  SetVolumeMute(DN:TDeviceName; Value:Boolean);

implementation

//获取音量
function GetVolume(DN:TDeviceName) : Word;
var
 hMix: HMIXER;
 mxlc: MIXERLINECONTROLS;
 mxcd: TMIXERCONTROLDETAILS;
 vol: TMIXERCONTROLDETAILS_UNSIGNED;
 mxc: MIXERCONTROL;
 mxl: TMixerLine;
 intRet: Integer;
 nMixerDevs: Integer;
begin
 // Check if Mixer is available
 nMixerDevs := mixerGetNumDevs();
 if (nMixerDevs < 1) then
 begin
   Exit;
 end;

 // open the mixer
 intRet := mixerOpen(@hMix, 0, 0, 0, 0);
 if intRet = MMSYSERR_NOERROR then
 begin
   case DN of
     Master :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
     Microphone :
             mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
     WaveOut : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
     Synth  :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
   end;
   mxl.cbStruct := SizeOf(mxl);

   // get line info
   intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

   if intRet = MMSYSERR_NOERROR then
   begin
     FillChar(mxlc, SizeOf(mxlc),0);
     mxlc.cbStruct := SizeOf(mxlc);
     mxlc.dwLineID := mxl.dwLineID;
     mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
     mxlc.cControls := 1;
     mxlc.cbmxctrl := SizeOf(mxc);

     mxlc.pamxctrl := @mxc;
     intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

     if intRet = MMSYSERR_NOERROR then
     begin
       FillChar(mxcd, SizeOf(mxcd),0);
       mxcd.dwControlID := mxc.dwControlID;
       mxcd.cbStruct := SizeOf(mxcd);
       mxcd.cMultipleItems := 0;
       mxcd.cbDetails := SizeOf(Vol);
       mxcd.paDetails := @vol;
       mxcd.cChannels := 1;

       intRet := mixerGetControlDetails(hMix, @mxcd,MIXER_SETCONTROLDETAILSF_VALUE);

       Result := vol.dwValue ;

       if intRet <> MMSYSERR_NOERROR then
         ShowMessage('GetControlDetails Error');
     end
     else
       ShowMessage('GetLineInfo Error');
   end;
   intRet := mixerClose(hMix);
 end;
end;

//设置音量
procedure setVolume(DN:TDeviceName; Value : Word);
var
 hMix: HMIXER;
 mxlc: MIXERLINECONTROLS;
 mxcd: TMIXERCONTROLDETAILS;
 vol: TMIXERCONTROLDETAILS_UNSIGNED;
 mxc: MIXERCONTROL;
 mxl: TMixerLine;
 intRet: Integer;
 nMixerDevs: Integer;
begin
 // Check if Mixer is available
 nMixerDevs := mixerGetNumDevs();
 if (nMixerDevs < 1) then
 begin
   Exit;
 end;

 // open the mixer
 intRet := mixerOpen(@hMix, 0, 0, 0, 0);
 if intRet = MMSYSERR_NOERROR then
 begin
   case DN of
     Master :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
     Microphone :
             mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
     WaveOut : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
     Synth  :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
   end;
   mxl.cbStruct := SizeOf(mxl);

   // get line info
   intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

   if intRet = MMSYSERR_NOERROR then
   begin
     FillChar(mxlc, SizeOf(mxlc),0);
     mxlc.cbStruct := SizeOf(mxlc);
     mxlc.dwLineID := mxl.dwLineID;
     mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
     mxlc.cControls := 1;
     mxlc.cbmxctrl := SizeOf(mxc);

     mxlc.pamxctrl := @mxc;
     intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

     if intRet = MMSYSERR_NOERROR then
     begin
       FillChar(mxcd, SizeOf(mxcd),0);
       mxcd.dwControlID := mxc.dwControlID;
       mxcd.cbStruct := SizeOf(mxcd);
       mxcd.cMultipleItems := 0;
       mxcd.cbDetails := SizeOf(Vol);
       mxcd.paDetails := @vol;
       mxcd.cChannels := 1;

       vol.dwValue := Value;

       intRet := mixerSetControlDetails(hMix, @mxcd,MIXER_SETCONTROLDETAILSF_VALUE);

       if intRet <> MMSYSERR_NOERROR then
         ShowMessage('SetControlDetails Error');
     end
     else
       ShowMessage('GetLineInfo Error');
   end;
   intRet := mixerClose(hMix);
 end;
end;

//获取静音
function  GetVolumeMute(DN:TDeviceName) : Boolean;
var
 hMix: HMIXER;
 mxlc: MIXERLINECONTROLS;
 mxcd: TMIXERCONTROLDETAILS;
 vol: TMIXERCONTROLDETAILS_UNSIGNED;
 mxc: MIXERCONTROL;
 mxl: TMixerLine;
 intRet: Integer;
 nMixerDevs: Integer;
 mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
 // Check if Mixer is available
 nMixerDevs := mixerGetNumDevs();
 if (nMixerDevs < 1) then
 begin
   Exit;
 end;

 // open the mixer
 intRet := mixerOpen(@hMix, 0, 0, 0, 0);
 if intRet = MMSYSERR_NOERROR then
 begin
   case DN of
     Master :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
     Microphone :
             mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
     WaveOut : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
     Synth  :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
   end;
    mxl.cbStruct        := SizeOf(mxl);

   // mixerline info
   intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

   if intRet = MMSYSERR_NOERROR then
   begin
     FillChar(mxlc, SizeOf(mxlc),0);
     mxlc.cbStruct := SizeOf(mxlc);
     mxlc.dwLineID := mxl.dwLineID;
     mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
     mxlc.cControls := 1;
     mxlc.cbmxctrl := SizeOf(mxc);
     mxlc.pamxctrl := @mxc;

     // Get the mute control
     intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

     if intRet = MMSYSERR_NOERROR then
     begin
       FillChar(mxcd, SizeOf(mxcd),0);
       mxcd.cbStruct := SizeOf(TMIXERCONTROLDETAILS);
       mxcd.dwControlID := mxc.dwControlID;
       mxcd.cChannels := 1;
       mxcd.cbDetails := SizeOf(MIXERCONTROLDETAILS_BOOLEAN);
       mxcd.paDetails := @mcdMute;

       // Get  mute
       intRet := mixerGetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

       if mcdMute.fValue = 0 then Result:=false
       else Result := True;

       if intRet <> MMSYSERR_NOERROR then
         ShowMessage('SetControlDetails Error');
     end
     else
       ShowMessage('GetLineInfo Error');
   end;

   intRet := mixerClose(hMix);
 end;
end;

//设置静音
procedure  SetVolumeMute(DN:TDeviceName; Value:Boolean);
var
 hMix: HMIXER;
 mxlc: MIXERLINECONTROLS;
 mxcd: TMIXERCONTROLDETAILS;
 vol: TMIXERCONTROLDETAILS_UNSIGNED;
 mxc: MIXERCONTROL;
 mxl: TMixerLine;
 intRet: Integer;
 nMixerDevs: Integer;
 mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
 // Check if Mixer is available
 nMixerDevs := mixerGetNumDevs();
 if (nMixerDevs < 1) then
 begin
   Exit;
 end;

 // open the mixer
 intRet := mixerOpen(@hMix, 0, 0, 0, 0);
 if intRet = MMSYSERR_NOERROR then
 begin
   case DN of
     Master :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
     Microphone :
             mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
     WaveOut : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
     Synth  :  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
   end;
    mxl.cbStruct        := SizeOf(mxl);

   // mixerline info
   intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

   if intRet = MMSYSERR_NOERROR then
   begin
     FillChar(mxlc, SizeOf(mxlc),0);
     mxlc.cbStruct := SizeOf(mxlc);
     mxlc.dwLineID := mxl.dwLineID;
     mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
     mxlc.cControls := 1;
     mxlc.cbmxctrl := SizeOf(mxc);
     mxlc.pamxctrl := @mxc;

     // Get the mute control
     intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

     if intRet = MMSYSERR_NOERROR then
     begin
       FillChar(mxcd, SizeOf(mxcd),0);
       mxcd.cbStruct := SizeOf(TMIXERCONTROLDETAILS);
       mxcd.dwControlID := mxc.dwControlID;
       mxcd.cChannels := 1;
       mxcd.cbDetails := SizeOf(MIXERCONTROLDETAILS_BOOLEAN);
       mxcd.paDetails := @mcdMute;

       // Set and UnSet  mute
       mcdMute.fValue := Ord(Value);
       intRet := mixerSetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

       if intRet <> MMSYSERR_NOERROR then
         ShowMessage('SetControlDetails Error');
     end
     else
       ShowMessage('GetLineInfo Error');
   end;

   intRet := mixerClose(hMix);
 end;
end;

end.

--------------------------------------------------------------------

unit Volume;

interface

uses windows,mmsystem;

type 
    Tvolume=record
       left,right:word; // 取值范围 0--65535
    end;

procedure fillstruct(control:PMixerControl;var Cdetails:tMIXERCONTROLDETAILS);
function getpeak(control:PMixerControl;var peak:integer):boolean;
function setvolume(control:Pmixercontrol; volume:Tvolume):boolean;
function getvolume(control:Pmixercontrol; volume:Tvolume):boolean;

var
    mcontrols:array of array of array of PMixerControl; //mixer的数组,单声卡可以不用
    fmixerhandle:HMIXER;   //mixer的句柄

implementation

procedure fillstruct(control:PMixerControl;var Cdetails:tMIXERCONTROLDETAILS);
begin
    Cdetails.cbStruct:=sizeof(cdetails);
    cdetails.dwControlID:=Control.dwControlID;
    cdetails.cbDetails:=sizeof(integer);
    cdetails.hwndOwner:=0;
end;

function getpeak(control:PMixerControl;var peak:integer):boolean;
var
   details:TMixerControlDetailsSigned;
   cdetails:tMIXERCONTROLDETAILS;
begin
    Result:=false;
    if control.dwControlType<> mixercontrol_controltype_peakmeter then exit;
    cdetails.cChannels:=1;
    cdetails.paDetails:=@details;
    fillstruct(control,cdetails);
    result:=mixerGetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
end;

function setvolume(control:Pmixercontrol; volume:Tvolume):boolean;
var
    details:array[0..30] of integer;
    cdetails:tMIXERCONTROLDETAILS;
begin
    fillstruct(control,cdetails);
    cdetails.cChannels:=2;
    cdetails.paDetails:=@details;
    details[0]:=volume.left;
    details[1]:=volume.right;
    result:=mixerSetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
    volume.left:=details[0];
    volume.right:=details[1];
end;

function getvolume(control:Pmixercontrol; volume:Tvolume):boolean;
var
    details:array[0..30] of integer;
    cdetails:tMIXERCONTROLDETAILS;
begin
    fillstruct(control,cdetails);
    cdetails.cChannels:=2;
    cdetails.paDetails:=@details;
    result:=mixerGetControlDetails(fmixerhandle,@cdetails,MIXER_GETCONTROLDETAILSF_VALUE)=0;
    volume.left:=details[0];
    volume.right:=details[1];
end;

end.

// 调用方法:
procedure TForm1.Button1Click(Sender: TObject);
var  s : Tvolume;
     kz : Pmixercontrol;
begin
    new(kz); //<------------ 此处一定要分配内存
kz.dwControlID := 0;
s.left := 0;
s.right := 0;
try
setvolume(kz,s);
except
end;
freemem(kz);
end;

  评论这张
 
阅读(2406)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017