Use directsound (6) in Delphi: Play by file format (twaveformatex)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.