写在打开文件的地方就行了
begin
var FMPOpenStruct:TFMP_OPEN_STRUCT;
begin
if openDialog1.Execute then
PPlayName:=openDialog1.FileName;
Fillchar(FMPOpenStruct,sizeof(TFMP_OPEN_STRUCT),0);
FMPOpenStruct.lpFileName:=Pchar(PPlayName);
FMPOpenStruct.dwCallBack:=DWORD(@FMPCallbackProc);
FMPEGStream:=FMPOpen(FMPF_BUFFERS,DWORD(@FMPOpenStruct));
FMPPlay(FMPEGStream,FMPF_POS_END or FMPF_END_STOP,0);
end;
Function FMPCallbackProc(bMsg: Byte;
hMPEGStream: Byte;
dwValue: DWORD): WORD stdcall;
var
Buf
Buf;
i:integer;
begin
Result:= 0;
try
Buf:= Pointer(FMPGet(hMPEGStream, FMPI_STM_USER));
case bMsg of
FMPM_BUF_CREATE: //创建缓冲区
begin
GetMem(Buf,sizeof(TBuf));
//Buf := GlobalAllocPtr(GMEM_FIXED,sizeof(TBuf));
if Buf=nil then
begin
//分配内存出错,可能是内存不足
Result:=FMPE_DOS;
Exit;
end;
Fillchar(Buf^,sizeof(TBUF),0);
Buf^.hFile:= CreateFile(pChar(PPlayName),//TFMP_OPEN_STRUCT(dwValue).lpFileName,
GENERIC_READ,FILE_SHARE_READ,Nil,OPEN_EXISTING,0,0);
if (Buf^.hFile=INVALID_HANDLE_VALUE) then
begin
//打开文件出错
FreeMemory(buf);
//GlobalFreePtr( Buf );
Result:=FMPE_DOS;
Exit;
end;
for i := 0 to MAX_BUF_NUMBER-1do
GetMem(Buf^.Buffer
,MAX_BUF_SIZE + 2);
//GlobalAllocPtr(GMEM_FIXED, );
Buf^.wIndex := 0;
Buf^.dwSize := 0;
//存储缓冲区结构到用户段
FMPSet(hMPEGStream,FMPI_STM_USER, DWORD(Buf) );
//在到缓冲区3/4时,触发填充下一个缓冲区
FMPSet(hMPEGStream, FMPI_BUF_POS, DWORD(Trunc(MAX_BUF_SIZE* 0.75)));
//FMPSet(hMPEGStream,FMPI_BUF_MODE,FMPF_BUF_LOOP);
end;
//关闭缓冲区
FMPM_BUF_CLOSE :
begin
CloseHandle(Buf^.hFile);
for i:=0 to MAX_BUF_NUMBER-1do
FreeMemory(Buf^.Buffer);
//GlobalFreePtr(Buf^.Buffer);
FreeMemory(Buf);
//GlobalFreePtr(Buf);
end;
//跳跃
FMPM_BUF_SEEK:
begin
SetFilePointer(Buf^.hFile,dwValue,nil,FILE_begin
);
end;
//读书据
FMPM_BUF_POS:
begin
ReadFile(Buf^.hFile,Buf^.Buffer[Buf^.wIndex]^,MAX_BUF_SIZE,Buf^.dwSize,nil);
end;
// 缓冲区空
FMPM_BUF_EMPTY :
begin
FMPSet(hMPEGStream, FMPI_BUF_SIZE,Buf^.dwSize );
FMPSet(hMPEGStream, FMPI_BUF_ADDRESS, DWORD(Buf^.Buffer[Buf^.wIndex]));
Inc(Buf^.wIndex);
if (Buf^.wIndex = MAX_BUF_NUMBER) then
Buf^.wIndex:=0;
//Buf^.wIndex := MAX_BUF_NUMBER MOD Buf^.wIndex;
end;
//错误
FMPM_ERROR:
begin
// if Assigned(FOnError) then
// FOnError(Self);
end;
//操作完成
FMPM_COMPLETED :
begin
if dwValue = 3 then
// 播完
begin
// if Assigned(FOnPlayOK) then
// FOnPlayOK(Self);
end;
end
end;
except
//异常
CloseHandle(Buf^.hFile);
for i:=0 to MAX_BUF_NUMBER-1do
FreeMemory(Buf^.Buffer);
FreeMemory(Buf);
end;
end;