Jump to content
giomach

Best way to play short sound file?

Recommended Posts

Windows 10, Delphi XE

 

I want to play a short sound file in response to clicking a button (var SoundButton:TBitBtn).  I have two ways which work, but neither is fully satisfactory.

 

Method 1: with a WAV file — fast, but needs a lot of disk space to store the files

procedure TFocalForm.SoundButtonClick(Sender: TObject);                         // 2024/03/20     for .WAV
var ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
    ms.LoadFromFile (SoundFileName);
    ms.Position := 0;
    sndPlaySound (ms.Memory, (SND_ASYNC or SND_MEMORY))
  finally
    ms.Free
  end
end;

Method 2: with a MP3 file (var SoundPlayer: TMediaPlayer) — saves disk space but there is a delay of several seconds before the sound starts

 

procedure TFocalForm.SoundButtonClick(Sender: TObject);                         // 2024/03/20     for .MP3
begin
SoundPlayer.Close;
SoundPlayer.Filename := SoundFileName;
SoundPlayer.Open;
SoundPlayer.Play;
end;

Is there a better way?

 

And a related question: these sound clips — there could be hundreds of thousands of them eventually — are extracted from a smaller number of larger sound files.  Is there an interface to play such a clip from the larger file, by supplying start and finish times?  And if there is, would it be any advantage at runtime?

 

Thanks for your suggestions.

 

Share this post


Link to post

Thanks for both suggestions, but in both cases it looks like a steep learning curve would be involved!  Lots of technical jargon to be deciphered!

 

Actually, I think TMediaPlayer can do all that I need.  I now realise that it can handle WAV as well as MP3, and that it will extract a clip, given start and end times.  It's just a matter of finding out how to do it all.

 

I intend adding these sound files to a Windows VCL app which I distribute, and I have some decisions to make: store the sounds locally or remotely; store them as WAV or MP3; store them as many separate files or extract them on-the-fly from longer files.  I'll just have to try them all and see what works best.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×