菜
菜鸟黄
Unregistered / Unconfirmed
GUEST, unregistred user!
//怎么我用下面的代码,没有声音输出呢?(1.txt中的内容是E:/音乐/情书.mp3)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
MMSystem, DirectSound, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
lpDirectSound: IDirectSound;
lpDirectSoundBuffer: IDirectSoundBuffer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
audiobuf: array[0..160000-1] of byte;
stream: TFileStream;
format: TWAVEFORMATEX;
BufferDesc: DSBUFFERDESC;
ptr1, ptr2: pointer;
pdw1, pdw2: dword;
begin
stream:=TFileStream.Create('1.txt',fmOpenRead);
stream.ReadBuffer(audiobuf, stream.Size);
fillchar(format, sizeof(format), 0);
format.wFormatTag:=WAVE_FORMAT_PCM;
format.nChannels:=1;
format.nSamplesPerSec:=8000;
format.nAvgBytesPerSec:=16000;
format.nBlockAlign:=2;
format.wBitsPerSample:=16;
fillchar(bufferdesc, sizeof(bufferdesc), 0);
bufferdesc.dwSize:=sizeof(bufferdesc);
bufferdesc.dwBufferBytes:=stream.Size;
bufferdesc.lpwfxFormat:=@format;
DirectSoundCreate(nil, lpDirectSound, nil);
lpDirectSound.SetCooperativeLevel(Handle, DSSCL_NORMAL);
lpDirectSound.CreateSoundBuffer(bufferdesc, lpDirectSoundBuffer, nil);
lpDirectSoundBuffer.Lock(0, stream.Size, ptr1, pdw1, ptr2, pdw2, 0);
copymemory(ptr1, @audiobuf, pdw1);
copymemory(ptr2, pchar(@audiobuf)+pdw1, pdw2);
lpDirectSoundBuffer.Unlock(ptr1, pdw1, ptr2, pdw2);
lpDirectSoundBuffer.Play(0, 0, 0);
stream.Free;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
MMSystem, DirectSound, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
lpDirectSound: IDirectSound;
lpDirectSoundBuffer: IDirectSoundBuffer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
audiobuf: array[0..160000-1] of byte;
stream: TFileStream;
format: TWAVEFORMATEX;
BufferDesc: DSBUFFERDESC;
ptr1, ptr2: pointer;
pdw1, pdw2: dword;
begin
stream:=TFileStream.Create('1.txt',fmOpenRead);
stream.ReadBuffer(audiobuf, stream.Size);
fillchar(format, sizeof(format), 0);
format.wFormatTag:=WAVE_FORMAT_PCM;
format.nChannels:=1;
format.nSamplesPerSec:=8000;
format.nAvgBytesPerSec:=16000;
format.nBlockAlign:=2;
format.wBitsPerSample:=16;
fillchar(bufferdesc, sizeof(bufferdesc), 0);
bufferdesc.dwSize:=sizeof(bufferdesc);
bufferdesc.dwBufferBytes:=stream.Size;
bufferdesc.lpwfxFormat:=@format;
DirectSoundCreate(nil, lpDirectSound, nil);
lpDirectSound.SetCooperativeLevel(Handle, DSSCL_NORMAL);
lpDirectSound.CreateSoundBuffer(bufferdesc, lpDirectSoundBuffer, nil);
lpDirectSoundBuffer.Lock(0, stream.Size, ptr1, pdw1, ptr2, pdw2, 0);
copymemory(ptr1, @audiobuf, pdw1);
copymemory(ptr2, pchar(@audiobuf)+pdw1, pdw2);
lpDirectSoundBuffer.Unlock(ptr1, pdw1, ptr2, pdw2);
lpDirectSoundBuffer.Play(0, 0, 0);
stream.Free;
end;
end.