سؤال

I am trying to make a program that plays sound files on every keypress in TEdit.

I tried sndPlaySound from a resource file on a Edit1 KeyPress event, but the problem is that it does not exactly play sounds as if in real keyboard typing. There's a delay in sound playing between every key pressed.

procedure TForm2.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
sndPlaySound(PCHAR('KeyPress'), SND_RESOURCE OR SND_ASYNC);
end;

This code does play sounds, but if you type quickly, only on the last key you type you'd hear the sound file being played.

هل كانت مفيدة؟

المحلول 2

The answer posted by TLama is this code which worked perfectly well:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Bass;

type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
StreamHandle: HSTREAM;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
if BASS_Init(-1, 44100, 0, Handle, nil) then
begin
StreamHandle := BASS_StreamCreateFile(False, PChar('c:\Windows\Media\tada.wav'), 0,
  0, 0 {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF});
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
BASS_StreamFree(StreamHandle);
BASS_Free;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
BASS_ChannelPlay(StreamHandle, True);
end;

end.

نصائح أخرى

You can use the WAVEOUT interface, but I believe this delay is some detail in the code that must be corrected. try to use the parameter SND_FILENAME.

the api is appropriate for what you're trying to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top