VafaK
VafaK

Reputation: 524

Read and play Wav file in delphi

How can I play uncompressed wav files in delphi? I mean reading the wav file byte by byte and sending the data directly to the speaker. I have searched the internet and found some functions for playing sound for a specified period of time. for example: myplaysoundfunc(1200, 100). this will play a sound of 1200Hz for 100 milliseconds. But playing a wav file is more complicated. I have problem with Samples per second and bits per samples.

Upvotes: 0

Views: 3205

Answers (1)

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24086

You don't need any components or external libraries for this.

Full application sourcecode, taken from http://rosettacode.org/wiki/Play_recorded_sounds#Delphi :

{$APPTYPE CONSOLE}
program PlayRecordedSounds;
uses MMSystem;
begin
  sndPlaySound('SoundFile.wav', SND_NODEFAULT OR SND_ASYNC);
end.

With these parameters the sound plays on the background while your app keeps running. If you use SND_SYNC instead of SND_ASYNC, your application waits for the sound to finish before continuing.

You can also let the sound loop if you add SND_LOOP. This procedure stops sounds that you've previously played, unless you add SND_NOSTOP. That way, the old sound keeps playing.

Notes:

  • This will only work on Windows. For FireMonkey there are other options.
  • If you really want to play sample-by-sample, things get more complicated. In that case I advise to use a library to help you out with that, like the Delphi ASIO/VST library.

Upvotes: 2

Related Questions