我发现了一个 Stack Overflow 帖子,其中包含一个示例,展示了如何获取 AVI 文件的持续时间:
我在我的 Delphi 6 应用程序中修改了它并创建了下面的代码。最初,我在调用 AviFileExit() 之前删除了核对 IAviFile 指针的行。但是当我这样做时,我在调用 AviFileExit() 时遇到了访问冲突。我恢复了线路,访问冲突消失了。
为什么在调用 AviFileExit() 之前需要核对 IAviFile 引用?这是内存泄漏吗?我认为正常的接口引用计数在这里可以正常工作,但显然不能。有没有另一种方法来消除错误,比如调用 AviStreamRelease() 之类的?
这是我的代码:
function getAviDurationSecs(theAviFilename: string): Extended;
var
aviFileInfo : TAVIFILEINFOW;
intfAviFile : IAVIFILE;
framesPerSecond : Extended;
begin
intfAviFile := nil;
AVIFileInit;
try
// Open the AVI file.
if AVIFileOpen(intfAviFile, PChar(theAviFilename), OF_READ, nil) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Error opening the AVI file: ' + theAviFilename);
try
// Get the AVI file information.
if AVIFileInfoW(intfAviFile, aviFileInfo, sizeof(aviFileInfo)) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Unable to get file information record from the AVI file: ' + theAviFilename);
// Zero divide protection.
if aviFileInfo.dwScale < 1 then
raise Exception.Create('(getAviDurationSecs) Invalid dwScale value found in the AVI file information record: ' + theAviFilename);
// Calculate the frames per second.
framesPerSecond := aviFileInfo.dwRate / aviFileInfo.dwScale;
Result := aviFileInfo.dwLength / framesPerSecond;
finally
AVIFileRelease(intfAviFile);
// Commenting out the line below that nukes the IAviFile
// interface reference leads to an access violation when
// AVIFileExit() is called.
Pointer(intfAviFile) := nil;
end;
finally
AVIFileExit;
end;
end;