操作 Wave 文件(10): 输入输出设备与格式支持



unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    ListBox1: TListBox;

    ListBox2: TListBox;

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses MMSystem;



//设备列表; 指定设备时经常使用 WAVE_MAPPER 参数, 这样会自动选用合适的设备.

procedure TForm1.Button1Click(Sender: TObject);

var

  i: Integer;

  waveOutCaps: TWaveOutCaps;

  waveInCaps: TWaveInCaps;

begin

  ListBox1.Items.Add('音频输出设备列表:');

  for i := 0 to waveOutGetNumDevs do

  begin

    ZeroMemory(@waveOutCaps, SizeOf(TWaveOutCaps));

    waveOutGetDevCaps(i, @waveOutCaps, SizeOf(TWaveOutCaps));

    ListBox1.Items.Add(waveOutCaps.szPname);

  end;



  ListBox2.Items.Add('音频输入设备列表:');

  for i := 0 to waveInGetNumDevs do

  begin

    ZeroMemory(@waveInCaps, SizeOf(TWaveInCaps));

    waveOutGetDevCaps(i, @waveInCaps, SizeOf(TWaveInCaps));

    ListBox2.Items.Add(waveInCaps.szPname);

  end;

end;



//判断是否支持指定的 Wave 格式

procedure TForm1.Button2Click(Sender: TObject);

var

  fmt: TPCMWaveFormat;

begin

  fmt.wf.wFormatTag := WAVE_FORMAT_PCM;

  fmt.wf.nChannels := 2;

  fmt.wf.nSamplesPerSec := 22050;

  fmt.wf.nAvgBytesPerSec := 88200;

  fmt.wf.nBlockAlign := 4;

  fmt.wBitsPerSample := 16;



  if waveOutOpen(nil, 0, PWaveFormatEx(@fmt), 0, 0, WAVE_FORMAT_QUERY) = 0 then

    ShowMessage('第一个输出设备支持此格式');



  if waveInOpen(nil, 0, PWaveFormatEx(@fmt), 0, 0, WAVE_FORMAT_QUERY) = 0 then

    ShowMessage('第一个输入设备支持此格式');

end;



end.


 
   

有把格式支持的判断写成函数的, 如:


function IsFormatSupported(fmt: Pointer; DeviceId: DWORD): Boolean;

begin

  Result := (waveOutOpen(nil, DeviceId, PWaveFormatEx(fmt), 0, 0, WAVE_FORMAT_QUERY) = 0);

end;


 
   

你可能感兴趣的:(文件)