程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 操作 Wave 文件(9): 使用 waveOut... 函數播放 wav 文件

操作 Wave 文件(9): 使用 waveOut... 函數播放 wav 文件

編輯:Delphi

 下面是使用低級音頻函數播放 wav 的兩個方法, 對這個感興趣的人恐怕很少, 免注釋了.

  使用窗口接受音頻輸出設備的消息:

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls; 
 
type 
 TForm1 = class(TForm) 
  Button1: TButton; 
  procedure Button1Click(Sender: TObject); 
 private 
 protected 
  procedure WndProc(var Message: TMessage); override; 
 public 
 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.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 := 0; 
 wh.dwLoops := 2; 
 wh.lpNext := nil; 
 wh.reserved := 0; 
 
 waveOutOpen(@hOut, WAVE_MAPPER, @fmt, Handle, 0, CALLBACK_WINDOW); 
 waveOutPrepareHeader(hOut, @wh, SizeOf(TWaveHdr)); 
 waveOutWrite(hOut, @wh, SizeOf(TWaveHdr)); 
end; 
 
procedure TForm1.WndProc(var Message: TMessage); 
begin 
 inherited; 
 case Message.Msg of 
  MM_WOM_OPEN: ; 
  MM_WOM_CLOSE: ; 
  MM_WOM_DONE: begin 
   waveOutUnprepareHeader(hOut, @wh, SizeOf(TWaveHdr)); 
   waveOutClose(hOut); 
  end; 
 end; 
end; 
 
end. 


 使用回調函數:

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 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 WaveProc(hWave: HWAVE; uMsg, dwInstance, dwParam1, dwParam2: DWord); stdcall; 
var 
 r: Integer; 
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 := 0; 
 wh.dwLoops := 2; 
 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; 
 
end. 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved