Search code examples
delphiaudiomedia-playerpascalwav

How to play multiple .wav files simultaneously in delphi


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?


Solution

  • 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?