views:

230

answers:

3

I wish to multiple .wav files simultaneously in delphi.

When I open and plat the first things are fine. However the second one causes a error when it tries to open.

It would appear that i can only use one media player at a time.... is there any way around this what do i do?

+2  A: 

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.

Wael Dalloul
A: 

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
SimaWB
That explains how to play them using an external executable. It doesn't answer the question, however, which was how to play multiple .wav files simultaneously **in Delphi**.
Ken White
+2  A: 

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?

Andreas Rejbrand
PlaySound with the SND_ASYNC should work just fine. I guessing the original poster use the TMediaPlayer component.
Ville Krumlinde
@VilleK: Just tried it. It does not work.
Andreas Rejbrand
Yes sorry you are right. I still haven't learn to never post anything before first verifying it myself :-)
Ville Krumlinde