我在NOTIFY里設了4個跟蹤點,但是WaitForMultipleObjects總是頻繁地觸發,且返回值總是等于WAIT_OBJECT_0,這是為什么?
哪位高手用過DelphiX的幫幫忙,謝謝了!
unit u_VoicePlayer;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,DXSounds,MMSystem,Wave,DirectX;
type
CVoicePlayer=class
private
  DS:TDXSound;
  DSBuf:TDirectSoundBuffer;
  g_Event:array [0..3] of THandle;
  PosNotify:array [0..3] of DSBPOSITIONNOTIFY;
  function CreateSBuf(Format: TWaveFormatEx; Size: Integer):boolean;
public
  constructor Create(DS0:TDXSound);
  destructor Free();
  procedure Play();
end;implementation
uses unit1;
constructor CVoicePlayer.Create(DS0:TDXSound);
var WaveFormat:TWaveFormatEx;i:integer;
    DSN:IDirectSoundNotify;
begin
  DS:=DS0;
  Ds.Initialize;
  DSBuf:=TDirectSoundBuffer.Create(Ds.DSound);
  MakePCMWaveFormatEx(WaveFormat,22050,16,2);
  createsbuf(WaveFormat,88200*4);
  DSBuf.IBuffer.QueryInterface(IID_IDirectSoundNotify,DSN);
  for i:=0 to 3 do begin
    PosNotify[i].dwOffset:=i*88200;
    g_Event[i]:=CreateEvent(nil,true,false,nil);
    PosNotify[i].hEventNotify:=g_Event[i];
  end;
  DSN.SetNotificationPositions(4,PosNotify);
  DSN:=nil;end;procedure CVoicePlayer.Play();
var res:dword;
begin
  DSBuf.IBuffer.SetCurrentPosition(0);
  DSBuf.IBuffer.Play(0,0,DSBPlay_LOOPING);
  while true do begin
    res:=WaitForMultipleObjects(4,PWOHandleArray(@g_Event[0]),false,INFINITE);
    if(res>=WAIT_OBJECT_0) then begin
      form1.lst.Items.Add(inttostr(res-WAIT_OBJECT_0)); //這里:res總是等于WAIT_OBJECT_0
    end;
  end;
end;destructor CVoicePlayer.Free();
var i:integer;
begin
  for i:=0 to 3 do begin
    closehandle(g_Event[i]);
  end;
  if DSBuf<>nil then begin
   DSBuf.Free();DSBuf:=nil;
  end;
  Ds.Finalize;
  DS:=nil;
end;function CVoicePlayer.CreateSBuf(Format: TWaveFormatEx; Size:Integer):boolean;
var
  BufferDesc: TDSBufferDesc;
begin
  FillChar(BufferDesc, SizeOf(BufferDesc), 0);
  with BufferDesc do
  begin
    dwSize := SizeOf(TDSBufferDesc);
    dwFlags := DSBCAPS_CTRLDEFAULT or DSBCAPS_GLOBALFOCUS or DSBCAPS_CTRLPOSITIONNOTIFY or DSBCAPS_GETCURRENTPOSITION2;
    dwBufferBytes := Size;
    lpwfxFormat := @Format;
  end;
  if not dsbuf.CreateBuffer(BufferDesc) then result:=false else result:=true;
end;
end.