问题来源: http://www.cnblogs.com/del/archive/2008/10/25/1069523.html#1351197
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses MMSystem;

{合并两个 Wav 文件流的函数}
function ConWavStream(AWavStream1, AWavStream2: TStream; var ANewStream: TStream): Boolean;
type
  TWavFormat = packed record
    ChunkID: array[0..3] of AnsiChar;     {'RIFF'}
    ChunkSize: Longword;                  {size-8}
    Format: array[0..3] of AnsiChar;      {'WAVE'}
    SubChunk1ID: array[0..3] of AnsiChar; {'fmt '}
    SubChunk1Size: Longword;              {hex10}
    AudioFormat: Word;                    {hex 01}
    NumOfChannels: Word;                  {1 mono, 2 stereo}
    SampleRate: Longword;                 {number of samples/sec}
    ByteRate: Longword;                   {samplerate* num of channels*bytes per (mono) sample}
    BytesperSample: Word;                 {size of (mono) sample}
    BitsPerSample: Word;                  {BytesperSample *8}
    SubChunk2ID: array[0..3] of AnsiChar; {'data'}
    SubChunk2Size: Longword;              {number of data bytes}
  end;
var
  vWavFormat1: TWavFormat;
  vWavFormat2: TWavFormat;
  vChunkSize1, vChunkSize2: Integer;
begin
  Result := False;
  if AWavStream1.Read(vWavFormat1, SizeOf(TWavFormat)) <> SizeOf(TWavFormat) then Exit;
  if AWavStream2.Read(vWavFormat2, SizeOf(TWavFormat)) <> SizeOf(TWavFormat) then Exit;
  if vWavFormat1.ChunkID <> 'RIFF' then Exit;
  if vWavFormat1.SubChunk2ID <> 'data' then Exit;
  vChunkSize1 := vWavFormat1.SubChunk2Size;
  vChunkSize2 := vWavFormat2.SubChunk2Size;
  vWavFormat1.ChunkSize := 0;
  vWavFormat1.SubChunk2Size := 0;
  vWavFormat2.ChunkSize := 0;
  vWavFormat2.SubChunk2Size := 0;
  if not CompareMem(@vWavFormat1, @vWavFormat2, SizeOf(TWavFormat)) then Exit; {格式不同}

  vWavFormat1.ChunkSize := vChunkSize1 + vChunkSize2 + SizeOf(vWavFormat1) - 8;
  vWavFormat1.SubChunk2Size := vChunkSize1 + vChunkSize2;
  ANewStream.Write(vWavFormat1, SizeOf(TWavFormat));
  ANewStream.CopyFrom(AWavStream1, vChunkSize1);
  ANewStream.CopyFrom(AWavStream2, vChunkSize2);

  Result := True;
end; { ConWavStream End}


var
  WavStream: TStream;

{合并两个资源流; 之前要在资源中分别加载两个 WAV 文件, 并分别命名: wav1、wav2}
procedure TForm1.FormCreate(Sender: TObject);
var
  rs1,rs2: TResourceStream;
begin
  rs1 := TResourceStream.Create(HInstance, 'wav1', RT_RCDATA);
  rs2 := TResourceStream.Create(HInstance, 'wav2', RT_RCDATA);
  WavStream := TMemoryStream.Create;
  ConWavStream(rs1, rs2, WavStream);
  rs1.Free;
  rs2.Free;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  WavStream.Free;
end;

{循环播放}
procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound(TMemoryStream(WavStream).Memory, SND_ASYNC or SND_MEMORY or SND_LOOP);
end;

{暂停}
procedure TForm1.Button2Click(Sender: TObject);
begin
  sndPlaySound(nil, 0);
end;

end.

posted on 2008-10-25 22:07  万一  阅读(3049)  评论(1编辑  收藏  举报