我一直在使用FindResource,LoadResource和LockResource来访问res文件中的资源.我有一个wave文件,我想通过我的Delphi应用程序提取和播放.
我没有提取就完成了,但这不是我想做的事情.我想先提取波形文件.谁能指出我正确的解决方案?
如果您已经打电话LoadResource和LockResource,那么你已经一半.LockResource为您提供指向资源数据的第一个字节的指针.调用SizeofResource以查找有多少字节,您可以使用该内存块执行任何操作,例如将其复制到另一个内存块或将其写入文件.
resinfo := FindResource(module, MakeIntResource(resid), type);
hres := LoadResource(module, resinfo);
pres := LockResource(module, hres);
// The following is the only new line in your code. You should
// already have code like the above.
size := SizeofResource(module, resinfo);
Run Code Online (Sandbox Code Playgroud)
复制到另一个内存块:
var
buffer: TBytes;
SetLength(buffer, size);
Move(pres^, buffer[0], size);
Run Code Online (Sandbox Code Playgroud)
写入文件:
var
fs: TStream;
fs := TFileStream.Create('foo.wav', fmCreate);
try
fs.Write(pres^, size);
finally
fs.Free;
end;
Run Code Online (Sandbox Code Playgroud)
这为我们提供了几种播放波形数据的方法:
PlaySound(MakeIntResource(resid), module, snd_Resource);
PlaySound(PChar(pres), 0, snd_Memory);
PlaySound(PChar(@buffer[0]), 0, snd_Memory);
PlaySound('foo.wav', 0, snd_FileName);
Run Code Online (Sandbox Code Playgroud)