奇怪的问题!求高手解释! (50分)

  • 主题发起人 主题发起人 bbtwo33
  • 开始时间 开始时间
B

bbtwo33

Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm1.GetWavLength(FileName : String): integer;
var Head : Array[0..43] of Char;
ReadFile : file;
NumRead, length : integer;

begin

length := 0;
AssignFile(ReadFile,FileName);
Reset(ReadFile,1);
BlockRead(ReadFile,Head,44,NumRead);

CopyMemory(@length,Head + 40,4);

CloseFile(ReadFile);

Result := length;
end;

这个函数就是想得到WAVE文件中声音数据的长度所以读前面44字节的文件头中的最后四个字节为声音文件的长度,然后将长度返回
执行到最后Result的时候有内存错误

改成下面这样就好了!
Reset(ReadFile,1);

seek(ReadFile,40);
blockread(ReadFile,datalength,4);

可是后来

if VoidOutInit = false then

begin

ListBox1.Items.Add('设备打开错误!');
exit;
end;

str := EdtPhoneNum.Text;

if str <> '' then

begin

NumCount := length(str);
SetLength(PhoneNum,NumCount);
SetLength(Temp,NumCount);
SetLength(LenList,NumCount);

for i := 0 to (NumCount - 1)do

begin

PhoneNum := str[i+1];
FileName := str[i+1] + '.wav';
m := GetWavLength(FileName);
LenList := m;
BufLen := BufLen + m;
GetMem(Temp,m + 1);
ZeroMemory(Temp,m + 1);
j := m;
AssignFile(ReadFile,FileName);
Reset(ReadFile,1);
Seek(ReadFile,44);
BlockRead(ReadFile,Temp,j,NumRead);
CloseFile(ReadFile);
end;

end;


*GetMem(PlayBuffer,BufLen);
ZeroMemory(PlayBuffer,BufLen);
for i := 0 to (NumCount-1)do

begin

CopyMemory(PlayBuffer + x,Temp,LenList);

x := x + LenList;
end;


这个程序就是想把WAVE文件里的声音数据拿出来
其中执行到*的地方出现内存读取错误,我发现m的值拿不到了!
这两个问题是不是有什么联系啊!帮我解决上面问题的大哥没有解释出错原因
我想知道是不是文件读取时的错误,哪个高手给解释解释啊!

对不起大家,实在是,没有分了!请各位包涵啊!
 
不是吧!都没人理啊!我知道分不多,求求大家了!帮个忙吧!
 
Reset前必须先写:
FileMode:= fmOpenRead;
Reset(ReadFile,1);
以防止打开只读文件发生错误。
不过我发现函数返回值都一样,为什么啊?
 
unit Unit1;

interface

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

type
//GD = array of Char;
TForm1 = class(TForm)
Button1: TButton;
EdtPhoneNum: TEdit;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
wf : TWaveFormatEx;
mmr : MMRESULT;
Flag : Boolean;
function VoidOutInit() : Boolean;
function GetWavLength(FileName : String): integer;
public
{ Public declarations }
end;


var
Form1: TForm1;
WaveOutHandle : HWAVEOUT;
implementation

{$R *.dfm}

{ TForm1 }

function TForm1.VoidOutInit: Boolean;
begin

wf.wFormatTag := WAVE_FORMAT_PCM;
wf.cbSize := 0;
wf.wBitsPerSample := 16;
wf.nSamplesPerSec := 16000;
wf.nChannels := 1;
wf.nAvgBytesPerSec := wf.nSamplesPerSec * ( wf.wBitsPerSample div 8 );
wf.nBlockAlign := wf.nChannels * ( wf.wBitsPerSample div 8 );

mmr := waveOutOpen(@WaveOutHandle,WAVE_MAPPER,@wf,self.Handle,0,CALLBACK_WINDOW);

if mmr <> MMSYSERR_NOERROR then

begin

result := false;
exit;
end;

result := true ;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin

Flag := False;
end;


procedure TForm1.Button1Click(Sender: TObject);
var ReadFile : file;
Buf : PWaveHdr;
//Temp : Array of GD;
Temp : Array of PChar;
//PlayBuffer : Array of Char;
PlayBuffer : PChar;
LenList : Array of integer;
PhoneNum : Array of Char;
NumRead, i, m, NumCount, BufLen, j,x: integer;
str,FileName : String;

begin

NumRead := 0;
BufLen := 0;
NumCount :=0;
x := 0;
m := 0;
if VoidOutInit = false then

begin

ListBox1.Items.Add('设备打开错误!');
exit;
end;

str := EdtPhoneNum.Text;

if str <> '' then

begin

NumCount := length(str);
SetLength(PhoneNum,NumCount);
SetLength(Temp,NumCount);
SetLength(LenList,NumCount);

for i := 0 to (NumCount - 1)do

begin

PhoneNum := str[i+1];
FileName := str[i+1] + '.wav';
m := GetWavLength(FileName);
LenList := m;
BufLen := BufLen + m;

//SetLength(Temp,m);
GetMem(Temp,m + 1);
ZeroMemory(Temp,m + 1);
ShowMessage(inttostr(m));

AssignFile(ReadFile,FileName);
Reset(ReadFile,1);
Seek(ReadFile,44);
BlockRead(ReadFile,Temp,m,NumRead);
CloseFile(ReadFile);
end;

end;

//SetLength(PlayBuffer,BufLen);

GetMem(PlayBuffer,BufLen);
ZeroMemory(PlayBuffer,BufLen);
for i := 0 to (NumCount-1)do

begin

CopyMemory(PlayBuffer + x ,Temp,LenList);

x := x + LenList;
end;

GetMem(Buf,sizeof(WaveHdr));
ZeroMemory(Buf,sizeof(WaveHdr));
GetMem(Buf.lpData,BufLen);
ZeroMemory(Buf.lpData,BufLen);

Buf.dwBufferLength := BufLen;
Buf.dwFlags := 0;
Buf.dwLoops := 0;
Buf.lpData := PlayBuffer;

ASSERT(waveOutPrepareHeader(WaveOutHandle, Buf,sizeof(WAVEHDR) ) = MMSYSERR_NOERROR);
ASSERT(waveOutWrite(WaveOutHandle ,Buf, sizeof(WAVEHDR) ) = MMSYSERR_NOERROR);


//GetMem(FileBuf,7052);
//ZeroMemory(FileBuf,7052);


//CopyMemory(Buf.lpData,FileBuf+44,7008);
//Buf.lpData := FileBuf + 44;//Temp + 44;




{AssignFile(ReadFile,'1.wav');
Reset(ReadFile,2);
BlockRead(ReadFile,FileBuf,7052,NumRead);
ASSERT(waveOutPrepareHeader(WaveOutHandle, Buf,sizeof(WAVEHDR) ) = MMSYSERR_NOERROR);
ASSERT(waveOutWrite(WaveOutHandle ,Buf, sizeof(WAVEHDR) ) = MMSYSERR_NOERROR);
CloseFile(ReadFile);
}


end;


function TForm1.GetWavLength(FileName : String): integer;
var //Head : Array[0..43] of Char;
ReadFile : file;
NumRead, len ,i: integer;

begin

len := 0;
AssignFile(ReadFile,FileName);
Reset(ReadFile,1);
seek(ReadFile,40);
//BlockRead(ReadFile,Head,44,NumRead);
BlockRead(ReadFile,len,4,NumRead);
//CopyMemory(@len,Head + 40,4);
CloseFile(ReadFile);
Result := len;
end;


end.



这是 全部的程序!其实就想把文件名为1到10的WAVE文件播放出来!现在里面有不少问题!大家帮帮忙!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
806
import
I
I
回复
0
查看
763
import
I
后退
顶部