讀取函數及測試代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses MMSystem;
function GetWaveData(FilePath: string; var stream: TMemoryStream): Boolean;
var
hFile: HMMIO;
ckiRIFF,ckiData: TMMCKInfo;
begin
Result := False;
hFile := mmioOpen(PChar(FilePath), nil, MMIO_READ);
if hFile = 0 then Exit;
ZeroMemory(@ckiRIFF, SizeOf(TMMCKInfo));
ZeroMemory(@ckiData, SizeOf(TMMCKInfo));
ckiData.ckid := mmiOStringToFOURCC('data', 0);
//先獲取主塊的信息
mmioDescend(hFile, @ckiRIFF, nil, MMIO_FINDRIFF);
//再獲取 data 塊的信息後, 指針將自動指向 data 數據的起點; 然後讀出數據
if (ckiRIFF.ckid = FOURCC_RIFF) and (ckiRIFF.fccType = mmiOStringToFOURCC('WAVE',0)) then
if mmioDescend(hFile, @ckiData, @ckiRIFF, MMIO_FINDCHUNK) = MMSYSERR_NOERROR then
begin
stream.Size := ckiData.cksize;
if mmioRead(hFile, stream.Memory, ckiData.cksize) = ckiData.cksize then Result := True;
end;
mmioClose(hFile, 0);
end;
//調用測試
procedure TForm1.Button1Click(Sender: TObject);
const
FilePath = 'C:\WINDOWS\Media\Windows XP 啟動.wav';
var
stream: TMemoryStream;
b: Byte;
str: string;
begin
stream := TMemoryStream.Create;
if GetWaveData(FilePath, stream) then
ShowMessageFmt('讀出的數據大小是: %d', [stream.Size]); {424600}
stream.Free;
end;
end.