问题
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?
回答1:
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 var
s with global threadvar
s.
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?
回答2:
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.
回答3:
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;
回答4:
if you want a real async wav file playing simulateously, and not interrupting other one, real solution there is mitov labs-audiolab. http://mitov.com/products/audiolab sndPlaySound ? not works mciSendString? not works as expected. thus, no simple winapi solution nor snd_async parameter etc works. only mitov audiolab worked so good. check it and remove thread solution as an answer. it doesn't working as simultaneously. one wave interrupts other one. mitov audiolab is the real solution that worked for me. so sharing here with ppl. hope this helps for someone.
回答5:
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
回答6:
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);
来源:https://stackoverflow.com/questions/3563974/how-to-play-multiple-wav-files-simultaneously-in-delphi