程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在 Delphi 下使用 DirectSound (8): IDirectSound8.DuplicateSoundBuffer() 與 IDirectSoundBuffer.GetStatus()

在 Delphi 下使用 DirectSound (8): IDirectSound8.DuplicateSoundBuffer() 與 IDirectSoundBuffer.GetStatus()

編輯:Delphi

  IDirectSoundBuffer.GetStatus() 用於獲取緩沖區的當前狀態, 譬如是否正在播放、是否指定為循環播放等等.

  IDirectSound8.DuplicateSoundBuffer() 是通過根據已存在的緩沖區建立緩沖區副本, 這類似引用; 通過它可以讓同一個聲音交叉播放.

  示例:

unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls; 
 
type 
  TForm1 = class(TForm) 
    Button1: TButton;    //播放 buf 
    Button2: TButton;    //播放 bufs[0] 
    Button3: TButton;    //播放 bufs[1] 
    Button4: TButton;    //全部停止 
    CheckBox1: TCheckBox; //控制是否循環播放 
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
    procedure Button3Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure Button4Click(Sender: TObject); 
    procedure CheckBox1Click(Sender: TObject); 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
uses DirectSound, MMSystem, ReadWave; //ReadWave 是前面(7)自定義的單元 
 
var 
  myDSound: IDirectSound8; 
  buf: IDirectSoundBuffer; //准備用這個緩沖區載入波形 
  bufs: array[0..1] of IDirectSoundBuffer; //這個緩沖區數組都將復制上面的 buf 
  loop: DWord = 0; //是否循環播放; 0:不循環; 1(DSBPLAY_LOOPING):循環 
 
{判斷緩沖區是否正在播放的函數} 
function IsPlay(b: IDirectSoundBuffer): Boolean; 
var 
  dStatus: DWord; 
begin 
  Result := b <> nil; 
  if Succeeded(b.GetStatus(dStatus)) then 
    Result := dStatus and DSBSTATUS_PLAYING = DSBSTATUS_PLAYING; 
end; 
 
{初始化設備} 
procedure TForm1.FormCreate(Sender: TObject); 
var 
  wavFormat: TWaveFormatEx; 
  bufDesc: TDSBufferDesc; 
  wavPath: string; 
  wav: TReadWave; 
  p1: Pointer; 
  n1: DWord; 
  i: Integer; 
begin 
  with TOpenDialog.Create(nil) do begin 
    Filter := 'Wave File(*.wav)|*.wav'; 
    if Execute then wavPath := FileName; 
    Free; 
  end; 
 
  wav := TReadWave.Create; 
  if not wav.Open(wavPath) then 
  begin 
    ShowMessage('打開失敗'); 
    wav.Free; 
    Application.Terminate; 
  end; 
 
  DirectSoundCreate8(nil, myDSound, nil); 
  myDSound.SetCoOperativeLevel(Handle, DSSCL_NORMAL); 
 
  ZeroMemory(@bufDesc, SizeOf(TDSBufferDesc)); 
  bufDesc.dwSize := SizeOf(TDSBufferDesc); 
  bufDesc.dwFlags := DSBCAPS_STATIC; 
  bufDesc.dwBufferBytes := wav.Size; 
  bufDesc.lpwfxFormat := @wav.Format; 
 
  {建立 buf 並載入波形} 
  myDSound.CreateSoundBuffer(bufDesc, buf, nil); 
  buf.Lock(0, 0, @p1, @n1, nil, nil, DSBLOCK_ENTIREBUFFER); 
  wav.Read(p1, n1); 
  wav.Free; 
  buf.Unlock(p1, n1, nil, 0); 
 
  {根據 buf 復制幾個緩沖區} 
  for i := Low(bufs) to High(bufs) do 
  begin 
    myDSound.DuplicateSoundBuffer(buf, bufs[i]); 
  end; 
end; 
 
{播放或暫停 buf} 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  if IsPlay(buf) then buf.Stop else buf.Play(0, 0, loop); 
end; 
 
{播放或暫停 buf[0]} 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  if IsPlay(bufs[0]) then buf.Stop else bufs[0].Play(0, 0, loop); 
end; 
 
{播放或暫停 buf[1]} 
procedure TForm1.Button3Click(Sender: TObject); 
begin 
  if IsPlay(bufs[1]) then buf.Stop else bufs[1].Play(0, 0, loop); 
end; 
 
{全部停止} 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  i: Integer; 
begin 
  if buf <> nil then buf.Stop; 
  for i := Low(bufs) to High(bufs) do if bufs[i] <> nil then bufs[i].Stop; 
end; 
 
{切換是否循環播放, 沒有對正在播放的緩沖區進行控制} 
procedure TForm1.CheckBox1Click(Sender: TObject); 
begin 
  loop := DWord(CheckBox1.Checked); 
end; 
 
procedure TForm1.FormDestroy(Sender: TObject); 
var 
  i: Integer; 
begin 
  buf := nil; 
  for i := Low(bufs) to High(bufs) do bufs[i] := nil; 
  myDSound := nil; 
end; 
 
end. 


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