The primary buffer must be created manually.
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure formcreate (Sender: tobject); Procedure formdestroy (Sender: tobject); Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses directsound, mmsystem, readwavefile; // readwavefile is a self-defined unit va in front (4 ). R mydsound: idirectsound8; // device object bufprimary: idirectsoundbuffer; // master buffer Buf: cached; // buffer {initialize the device and create the master buffer} procedure tform1.formcreate (Sender: tobject); var wavformat: twaveformatex; bufdesc: tdsbufferdesc; begin directsoundcreate8 (nil, mydsound, nil); mydsound. setcooperativelevel (handle, dsscl_priority); zeromemory (@ bufdesc, sizeof (tdsbufferdesc); bufdesc. dwsize: = sizeof (tdsbufferdesc ); Bufdesc. dwflags: = dsbcaps_primarybuffer; bufdesc. dwbufferbytes: = 0; bufdesc. lpwfxformat: = nil; mydsound. callback (bufdesc, bufprimary, nil); end; {create a buffer and populate data from the wave file} procedure success (Sender: tobject); var wavformat: twaveformatex; bufdesc: tdsbufferdesc; wavpath: string; WAV: treadwavefile; P1: pointer; N1: DWORD; begin Buf: = nil; with topendialog. create (NiL) Do begin filt Er: = 'wave file (*. wav) | *. wav '; If execute then wavpath: = filename; free; end; WAV: = treadwavefile. create; if not WAV. open (wavpath) then begin showmessage ('only PCM-format wave file'); WAV. free; exit; end; zeromemory (@ bufdesc, sizeof (tdsbufferdesc); bufdesc. dwsize: = sizeof (tdsbufferdesc); bufdesc. dwflags: = dsbcaps_static; bufdesc. dwbufferbytes: = WAV. size; bufdesc. lpwfxformat: = @ WAV. format; {set Buffer And change the format of the master buffer to the current file format} mydsound. createsoundbuffer (bufdesc, Buf, nil); bufprimary. setformat (@ wavformat );//! Buf. lock (0, 0, @ P1, @ N1, nil, nil, dsblock_entirebuffer); WAV. read (P1, N1); WAV. free; Buf. unlock (P1, N1, nil, 0); Buf. play (0, 0, 0); end; Procedure tform1.formdestroy (Sender: tobject); begin bufprimary: = nil; Buf: = nil; mydsound: = nil; end.