giomach 1 Posted March 21 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
DelphiUdIT 176 Posted March 21 (edited) Try to use PasLibVlc ... can play whatever you want and it's fast (you should have installed VLC). You have the full control on the stream media. https://prog.olsztyn.pl/paslibvlc/ Edited March 21 by DelphiUdIT Share this post Link to post
giomach 1 Posted March 23 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