2004. szeptember 5., vasárnap

How to play two sounds simultaneously


Problem/Question/Abstract:

I'd like to have a background.wav file playing and once in a while have a short effects .wav sound play at the same time (without interrupting - stopping and restarting it - the background .wav).

Answer:

The Delphi code below plays two sounds concurrently under Win95. I think concurrent sound playing relies on the driver supporting multiple inputs, and I believe you could request if it has this capability before trying to play.


procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMCICommand('open waveaudio shareable');
  SendMCICommand('play hickory.wav');
  SendMCICommand('play greeting.wav');
  SendMCICommand('close waveaudio');
end;

procedure TForm1.SendMCICommand(Cmd: string);
var
  RetVal: integer;
  ErrMsg: array[0..254] of char;
begin
  RetVal := mciSendString(StrAsPChar(Cmd), nil, 0, 0);
  if RetVal <> 0 then
  begin
    {get message for returned value}
    mciGetErrorString(RetVal, ErrMsg, 255);
    MessageDlg(StrPas(ErrMsg), mtError, [mbOK], 0);
  end;
end;

function StrAsPChar(var S: OpenString): PChar;
{returns a PChar from a string}
begin
  if Length(S) = High(S) then
    dec(S[0]);
  S[Ord(Length(s)) + 1] := #0;
  Result := @S[1];
end;

Nincsenek megjegyzések:

Megjegyzés küldése