如何能控制系统音量(125分)

  • 主题发起人 主题发起人 jzg
  • 开始时间 开始时间
J

jzg

Unregistered / Unconfirmed
GUEST, unregistred user!
用什莫控件或函数能控制集成声卡的主板的系统音量。谢谢个位。
最好有源码。我还可以加分
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=232460
http://www.delphibbs.com/delphibbs/dispq.asp?lid=429017
http://www.delphibbs.com/delphibbs/dispq.asp?lid=269359
http://www.delphibbs.com/delphibbs/dispq.asp?lid=363616
http://www.delphibbs.com/delphibbs/dispq.asp?lid=93092
http://www.delphibbs.com/delphibbs/dispq.asp?lid=465342

当你做一个多媒体播放器时,难免少不了控制音量的大小和左右声道的播放,下面就介绍一种控制Wave波形输出设备音量的方法,该方法不是设置主音量。先在窗体上放两个TTrackBar,分别命名为TrackBar1,TrackBar2,属性Max都设置为65535,如果觉得刻度太密了,可以把Frequency属性值设置大一些,然后在Uses段加入MMSystem,并在TrackBar1和TrackBar2的OnChange事件都写上下列语句:

procedure TForm1.TrackBar1Change(Sender: Tobject);
var Wave:string;
begin

Wave:='$'+inttohex(TrackBar1.Position,4)+inttohex(TrackBar2.Position,4);
waveoutsetvolume(0,strtoint(Wave));
end;

/////////////////////////////////////////////
WaveOutSetVolume(hwo: Integer;
dwVolume: Cardinal);

hwo is MediaPlayer1.DeviceId,

example: Right// | Left//
dwVolume for Full L+R = $FFFFFFFF
dwVolume for Full L no R = $0000FFFF
dwVolume for Full R no L = $FFFF0000
dwVolume for no sound = $00000000
/////////////////////////////////////////////////

uses MMSystem

type
TVolType = (vtLeft, vtRight);
TVol = array[vtLeft..vtRight] of word;

procedure TVolForm.GetVolumes(var DevId : word;
var VolLeft, VolRight : word);
{volume is returned as a pointer to a DWord with the most
significant word for the left channel. The channels are
extracted by treating the DWord as a two element array and
accessing the two array elements for the Lt and Rt volumes}
var
Error : integer;
MsgResponse : word;
TempVol : TVol;
begin

Error := AuxGetVolume(DevId, @TempVol);
if Error <> 0 then
begin

Timer1.Enabled := false;
MsgResponse := MessageDlg('Error Reading Volume : ' + IntToStr(Error) +
chr(13)
+ 'DevId : ' + IntToStr(DevId) + chr(13)
+ chr(13)
+ 'Set Next Device ?',
mtError, [mbYes, mbCancel], 0);
if MsgResponse = mrYes then
{try the next Device Id}
DevId := DevId + 1
else
begin

DevId := 0;
PChangeType := Nil;
end;

{else
MsgResponse = mrCancel}
Exit;
end;

{if Error <> 0}
VolLeft := TempVol[vtLeft];
VolRight := TempVol[vtRight];
end;


procedure TVolForm.SetVolumes(DevId : word;
var VolLeft, VolRight : word);
{volume is set by passing a DWord value with the most
significant word set for the left channel, and the least
significant word set for the right channel. The channels are
set by treating the DWord as a two element array and
setting the two array elements for the Lt and Rt volumes}
var
Error : integer;
TempVol : TVol;
begin

TempVol[vtLeft] := VolLeft;
TempVol[vtRight] := VolRight;
Error := AuxSetVolume(DevId, longint(TempVol));
if Error <> 0 then

MessageDlg('Error Setting Volume : ' + IntToStr(Error),
mtError, [mbOK], 0);
end;



 
看看电脑报2000年的合定本,上面有更简单的方法。
 
BOOL SetWaveOutVolume(unsigned long dwVolume)
{
UINT uRetVal, uNumDevs;
WAVEOUTCAPS waveCaps;

uNumDevs = waveOutGetNumDevs();
if (uNumDevs<=0)
return FALSE;

if (!waveOutGetDevCaps(0,(LPWAVEOUTCAPS)&amp;waveCaps,sizeof(WAVEOUTCAPS)))
{
// Verify the device supports volume changes
if(waveCaps.dwSupport &amp;
WAVECAPS_VOLUME)
{
// The low word is the left volume, the high word is the right.
uRetVal = waveOutSetVolume(0, dwVolume);
if(uRetVal == MMSYSERR_NOERROR)
{ return TRUE;
}
}
}

return FALSE;
}

在onChange 中:

begin

SetWaveOutVolume(aTraceBar.Position);
end;




uses mmsystem;

//得到当前音量:
procedure GetVolume;
var allvolume,leftv,rightv:integer;
begin

waveoutgetvolume(0,@allvolume);
leftv:=allvolume and $0ffff;
rightv:=(allvolume and $0ffff0000) div $10000;
end;


//设置当前音量:
procedure SetVolume(left,right:integer);
var leftv,rightv:integer;
begin

leftv:=left;
rightv:=right;
mmsystem.waveOutSetVolume(0,leftv+rightv*65536);
end;

//********************************
procedure TForm1.TrackBarrChange(Sender: TObject);
var
pos,vol:longint;
begin

pos:=TrackBarr.Position;
waveoutgetvolume(0,@vol);
vol:=vol and $ffff0000 or (pos shl 8);
waveoutsetvolume(0,vol);
end;


procedure TForm1.TrackBar1Change(Sender: TObject);
var
pos,vol:longint;
begin

pos:=TrackBar1.Position;
waveoutgetvolume(0,@vol);
vol:=vol and $0000ffff or (pos shl 24);
waveoutsetvolume(0,vol);
end;


 
说什么也该给分了
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部