测试程序:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}uses DirectSound, MMSystem;var myDSound: IDirectSound8; //设备对象 bufPrimary: IDirectSoundBuffer; //主缓冲 buf: IDirectSoundBuffer; //次缓冲 buf8: IDirectSoundBuffer8; //次缓冲的 IDirectSoundBuffer8 接口{初始化设备}procedure TForm1.FormCreate(Sender: TObject);begin DirectSoundCreate8(nil, myDSound, nil); {若手动建立主缓冲, 设备的优先级至少要指定为 DSSCL_PRIORITY} myDSound.SetCooperativeLevel(Handle, DSSCL_PRIORITY);end;{建立主缓冲, 并修改其格式}procedure TForm1.Button1Click(Sender: TObject);var wavFormat,fmt2: TWaveFormatEx; bufDesc: TDSBufferDesc;begin ZeroMemory(@bufDesc, SizeOf(TDSBufferDesc)); bufDesc.dwSize := SizeOf(TDSBufferDesc); bufDesc.dwFlags := DSBCAPS_PRIMARYBUFFER; //指明建立的是主缓冲 bufDesc.dwBufferBytes := 0; //主缓冲有固定的大小, 无需指定, 须是 0 bufDesc.lpwfxFormat := nil; //主缓冲有自己的格式, 修改它须通过 SetFormat() 方法 myDSound.CreateSoundBuffer(bufDesc, bufPrimary, nil); {显示修改前主缓冲格式} bufPrimary.GetFormat(@fmt2, SizeOf(TWaveFormatEx), nil); ShowMessageFmt('主缓冲默认: %dHZ %d 位 %d 声道', [fmt2.nSamplesPerSec, fmt2.wBitsPerSample, fmt2.nChannels]); {修改主缓冲的格式} ZeroMemory(@wavFormat, SizeOf(TWaveFormatEx)); with wavFormat do begin wFormatTag := WAVE_FORMAT_PCM; nChannels := 2; nSamplesPerSec := 44100; wBitsPerSample := 16; nBlockAlign := wBitsPerSample * nChannels div 8; nAvgBytesPerSec := nSamplesPerSec * nBlockAlign; end; bufPrimary.SetFormat(@wavFormat); {显示修改后主缓冲格式} bufPrimary.GetFormat(@fmt2, SizeOf(TWaveFormatEx), nil); ShowMessageFmt('主缓冲改后: %dHZ %d 位 %d 声道', [fmt2.nSamplesPerSec, fmt2.wBitsPerSample, fmt2.nChannels]);end;{建立次缓冲, 同时获取个 IDirectSoundBuffer8 接口}procedure TForm1.Button2Click(Sender: TObject);var wavFormat,fmt2: TWaveFormatEx; bufDesc: TDSBufferDesc;begin {为建立次缓冲准备格式} ZeroMemory(@wavFormat, SizeOf(TWaveFormatEx)); with wavFormat do begin wFormatTag := WAVE_FORMAT_PCM; nChannels := 2; nSamplesPerSec := 44100; wBitsPerSample := 16; nBlockAlign := wBitsPerSample * nChannels div 8; nAvgBytesPerSec := nSamplesPerSec * nBlockAlign; end; ZeroMemory(@bufDesc, SizeOf(TDSBufferDesc)); bufDesc.dwSize := SizeOf(TDSBufferDesc); bufDesc.dwFlags := DSBCAPS_STATIC; bufDesc.dwBufferBytes := 3 * wavFormat.nAvgBytesPerSec; //指定容纳 3 秒钟的波形数据 bufDesc.lpwfxFormat := @wavFormat; {建立 IDirectSoundBuffer, 并查看其格式} myDSound.CreateSoundBuffer(bufDesc, buf, nil); buf.GetFormat(@fmt2, SizeOf(TWaveFormatEx), nil); ShowMessageFmt('buf: %dHZ %d 位 %d 声道', [fmt2.nSamplesPerSec, fmt2.wBitsPerSample, fmt2.nChannels]); {从 IDirectSoundBuffer 获取 IDirectSoundBuffer8, 并查看其格式} buf.QueryInterface(IID_IDirectSoundBuffer8, buf8); // ZeroMemory(@fmt2, SizeOf(TWaveFormatEx)); buf.GetFormat(@fmt2, SizeOf(TWaveFormatEx), nil); ShowMessageFmt('buf8: %dHZ %d 位 %d 声道', [fmt2.nSamplesPerSec, fmt2.wBitsPerSample, fmt2.nChannels]);end;procedure TForm1.FormDestroy(Sender: TObject);begin bufPrimary := nil; buf := nil; buf8 := nil; myDSound := nil;end;end.