操作 Wave 文件(12): 使用 waveOut...重复播放 wav 文件
操作 Wave 文件(12): 使用 waveOut...重复播放 wav 文件
涂孟超 发表于3年前
操作 Wave 文件(12): 使用 waveOut...重复播放 wav 文件
  • 发表于 3年前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses MMSystem;

function GetWaveFmtData(path: string; var fmt: TWaveFormatEx; var buf: TBytes): Boolean;
var
  hFile: HMMIO;
  ckiRIFF,ckiFmt,ckiData: TMMCKInfo;
begin
  Result := False;
  hFile := mmioOpen(PChar(path), nil, MMIO_READ);
  if hFile = 0 then Exit;

  ZeroMemory(@ckiRIFF, SizeOf(TMMCKInfo));
  ZeroMemory(@ckiFmt, SizeOf(TMMCKInfo));
  ZeroMemory(@ckiData, SizeOf(TMMCKInfo));

  ckiRIFF.fccType := mmioStringToFOURCC('WAVE', 0);
  ckiFmt.ckid := mmioStringToFOURCC('fmt', 0);
  ckiData.ckid := mmioStringToFOURCC('data', 0);

  ZeroMemory(@fmt, SizeOf(TWaveFormatEx));

  mmioDescend(hFile, @ckiRIFF, nil, MMIO_FINDRIFF);

  if (ckiRIFF.ckid = FOURCC_RIFF) and (ckiRIFF.fccType = mmioStringToFOURCC('WAVE',0)) and
     (mmioDescend(hFile, @ckiFmt, @ckiRIFF, MMIO_FINDCHUNK) = MMSYSERR_NOERROR) and
     (mmioRead(hFile, @fmt, ckiFmt.cksize) = ckiFmt.cksize) and
     (mmioAscend(hFile, @ckiFmt, 0) = MMSYSERR_NOERROR) and
     (mmioDescend(hFile, @ckiData, @ckiRIFF, MMIO_FINDCHUNK) = MMSYSERR_NOERROR) then
  begin
    SetLength(buf, ckiData.cksize);
    Result := (mmioRead(hFile, PAnsiChar(buf), ckiData.cksize) = ckiData.cksize);
  end;

  mmioClose(hFile, 0);
end;

//------------------------------------------------------------------------------
var
  wh: TWaveHdr;
  hOut: HWAVEOUT;
  fmt: TWaveFormatEx;
  buf: TBytes;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Caption := '打开并播放';
  Button2.Caption := '暂停';
  Button3.Caption := '继续';
end;

procedure WaveProc(hWave: HWAVE; uMsg, dwInstance, dwParam1, dwParam2: DWORD); stdcall;
begin
  case uMsg of
    MM_WOM_OPEN: ;
    MM_WOM_CLOSE: ;
    MM_WOM_DONE: begin
      waveOutUnprepareHeader(hWave, PWaveHdr(dwParam1), SizeOf(TWaveHdr));
      waveOutClose(hWave);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  path = 'C:\WINDOWS\Media\Windows XP 启动.wav';
begin
  GetWaveFmtData(path, fmt, buf);

  wh.lpData := PAnsiChar(buf);
  wh.dwBufferLength := Length(buf);
  wh.dwBytesRecorded := 0;
  wh.dwUser := 0;
  wh.dwFlags := WHDR_BEGINLOOP or WHDR_ENDLOOP; {关键设置}
  wh.dwLoops := 3;                              {重复播放的次数}
  wh.lpNext := nil;
  wh.reserved := 0;

  waveOutOpen(@hOut, WAVE_MAPPER, @fmt, DWORD(@WaveProc), 0, CALLBACK_FUNCTION);
  waveOutPrepareHeader(hOut, @wh, SizeOf(TWaveHdr));
  waveOutWrite(hOut, @wh, SizeOf(TWaveHdr));
end;

//暂停
procedure TForm1.Button2Click(Sender: TObject);
begin
  waveOutPause(hOut);
end;

//继续
procedure TForm1.Button3Click(Sender: TObject);
begin
  waveOutRestart(hOut);
end;

end.

 
 
 
 
 

 

 

  
共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: