How to play multiple .wav files simultaneously in delphi

家住魔仙堡 提交于 2019-11-30 16:26:34
Andreas Rejbrand

How would you play a single sound? When I want fine control, I use the waveOut functions, as in this answer. My answer there also allows you to play the sound using a thread (that is, among other things, asynchronically). I think that you can play two sounds at the same time, by simply starting two such threads at the same time, if you only replace global vars with global threadvars.

Update

The simplest way to play a single sound is to use PlaySound. This can be used asynchronically, but since you ask this question, I assume that this does not allow you to use this function twice in a row to start simultaneous playback of two files. But: If you create a thread that only plays the sound (synchronically so that the thread doesn't die before the playback is finnished), then you can probably use two such threads to play two audio files simultaneously. (I have no access to a Delphi compiler right now, so I am afraid that I cannot test my hypotheses.)

Update 2

My hypothesis was that you could use two calls to PlaySound if only the function was called from two different threads, but apparently that is not good enough. You really need two different processes, it seems, which is bad (to state the obvious). I tried

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TSoundPlayerThread.Create(true) do
  begin
    SetFileName('C:\Users\Andreas Rejbrand\Downloads\Anna.wav');
    FreeOnTerminate := true;
    Resume;
  end;

  with TSoundPlayerThread.Create(true) do
  begin
    SetFileName('C:\Users\Andreas Rejbrand\Downloads\Mike.wav');
    FreeOnTerminate := true;
    Resume;
  end;
end;

with

unit SoundPlayerThread;

interface

uses
  Classes, MMSystem, Windows;

type
  TSoundPlayerThread = class(TThread)
  private
    { Private declarations }
    FAudioFileName: string;
  protected
    procedure Execute; override;
  public
    procedure SetFileName(const FileName: string);
  end;

implementation

procedure TSoundPlayerThread.Execute;
begin
  PlaySound(PChar(FAudioFileName), 0, SND_SYNC);
end;

procedure TSoundPlayerThread.SetFileName(const FileName: string);
begin
  FAudioFileName := FileName;
end;

end.

and only the latter wave file was played.

Update 3

I have actually written a small WAV file library. Using this, I can load two WAV files, merge them, and send the result to the audio driver. However, it is too much code to post here. If I get time left some day I might write a more lightweight PlaySimultaneously procedure and post it.

Otherwise: DirectX?

You can use DirectShow transform filter it is a Microsoft Windows Application Programming Interface (API) that enables Windows applications to interact with and control Windows "Media" input devices,

Or you can use WaveMix DLL it is a utility that allows multiple WAV files to be played simultaneously. It is designed to be as simple to use as possible but still have the power to do what is required by games. The DLL supports 8 channels of simultaneous wave play, the ability to queue up waves along the same channel and wave completion notification.

I did it by doing something like this:

//yup, i used more than 1 MediaPlayer controls.
MediaPlayer1.FileName:='song1.wav';
MediaPlayer2.FileName:='song2.wav';
MediaPlayer1.Open;
MediaPlayer2.Open;
MediaPlayer1.Play;
MediaPlayer2.Play;


//the next lines of code is checked every so often in my Timer event handler.
if MediaPlayer1.Position=MediaPlayer1.Length then begin
       MediaPlayer1.Close;
end;

if MediaPlayer2.Position=MediaPlayer2.Length then begin
       MediaPlayer2.Close;
end;

i think we can using

sndPlaySound('filename1.wav',SND_SYN OR SND_NODEFAULT);
sndPlaySound('filename2.wav',SND_SYN OR SND_NODEFAULT);
sndPlaySound('filename3.wav',SND_SYN OR SND_NODEFAULT);
sndPlaySound('filename4.wav',SND_SYN OR SND_NODEFAULT);

You can use SoX (Sound eXchange) for this. It can play and record audio files, apply various effects, convert various formats....
Check -m parameter to play files simultaneously. Example:

sox -m 1.wav 2.wav -d
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!