通常,mp3文件或wmv文件都有包含了歌曲名称、歌曲演唱者、所在专辑等信息,这些信息在Windows Mediplayer、real One Player等常用软件中播放时,都能在它们的上面显示出来。请问如何用Delphi编程去读取Mp3文件或wmv文件的这些信息?可以回贴,也可以发邮箱过来:[email protected],都给分,不够可再加。
找到一个Mp3信息结构的定义,Delphi的代码不用我写吧 :)http://www.daima.com.cn/Info/97/Info31864/ 一首MP3的额外信息存放在文件的最后面,共占128个字节,其中包括以下的内容(我们定义一个结构说明): public struct Mp3Info { public string identify;//TAG,三个字节 public string Title;//歌曲名,30个字节 public string Artist;//歌手名,30个字节 public string Album;//所属唱片,30个字节 public string Year;//年,4个字符 public string Comment;//注释,28个字节 public char reserved1;//保留位,一个字节 public char reserved2;//保留位,一个字节 public char reserved3;//保留位,一个字节 }
唉,反正都写了,贴出来算了unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; OpenDialog1: TOpenDialog; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type TMp3Info=record identify:string;//TAG,三个字节 Title:string;//歌曲名,30个字节 Artist:string;//歌手名,30个字节 Album:string;//所属唱片,30个字节 Year:string;//年,4个字符 Comment:string;//注释,28个字节 reserved1:char;//保留位,一个字节 reserved2:char;//保留位,一个字节 reserved3:char;//保留位,一个字节 end; const Size=128; var Form1: TForm1;implementation{$R *.dfm}function GetMp3Info(FileName:string):TMp3Info; var Buf:array [0..Size-1] of char; hFile:THandle; nReal:DWORD; i:integer; begin hFile:=CreateFile(PChar(FileName),GENERIC_ALL,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0); SysUtils.FileSeek(hFile,-Size,soFromEnd); if ReadFile(hFile,Buf,Size,nReal,nil) then begin for i:=0 to Size-1 do if Buf[i]=#0 then Buf[i]:=' '; with Result do begin identify:=Copy(Buf,0,3); Title:=Copy(Buf,4,30); Artist:=Copy(Buf,34,30); Album:=Copy(Buf,64,30); Year:=Copy(Buf,94,4); Comment:=Copy(Buf,98,28); reserved1:=Buf[125]; reserved2:=Buf[126]; reserved3:=Buf[127]; end; end; CloseHandle(hFile); end;procedure TForm1.Button1Click(Sender: TObject); var Mp3Info:TMp3Info; begin if OpenDialog1.Execute then Mp3Info:=GetMp3Info(OpenDialog1.FileName); with Mp3Info do begin if identify='TAG' then ShowMessage('歌名:'+Title+#13+ '歌手:'+Artist+#13+ '专辑:'+Album+#13+ '年份:'+Year+#13+ '备注:'+Comment); end; end;end.
一首MP3的额外信息存放在文件的最后面,共占128个字节,其中包括以下的内容(我们定义一个结构说明): public struct Mp3Info { public string identify;//TAG,三个字节 public string Title;//歌曲名,30个字节 public string Artist;//歌手名,30个字节 public string Album;//所属唱片,30个字节 public string Year;//年,4个字符 public string Comment;//注释,28个字节 public char reserved1;//保留位,一个字节 public char reserved2;//保留位,一个字节 public char reserved3;//保留位,一个字节 }
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMp3Info=record
identify:string;//TAG,三个字节
Title:string;//歌曲名,30个字节
Artist:string;//歌手名,30个字节
Album:string;//所属唱片,30个字节
Year:string;//年,4个字符
Comment:string;//注释,28个字节
reserved1:char;//保留位,一个字节
reserved2:char;//保留位,一个字节
reserved3:char;//保留位,一个字节
end;
const Size=128;
var
Form1: TForm1;implementation{$R *.dfm}function GetMp3Info(FileName:string):TMp3Info;
var
Buf:array [0..Size-1] of char;
hFile:THandle;
nReal:DWORD;
i:integer;
begin
hFile:=CreateFile(PChar(FileName),GENERIC_ALL,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0);
SysUtils.FileSeek(hFile,-Size,soFromEnd);
if ReadFile(hFile,Buf,Size,nReal,nil) then
begin
for i:=0 to Size-1 do
if Buf[i]=#0 then Buf[i]:=' ';
with Result do
begin
identify:=Copy(Buf,0,3);
Title:=Copy(Buf,4,30);
Artist:=Copy(Buf,34,30);
Album:=Copy(Buf,64,30);
Year:=Copy(Buf,94,4);
Comment:=Copy(Buf,98,28);
reserved1:=Buf[125];
reserved2:=Buf[126];
reserved3:=Buf[127];
end;
end;
CloseHandle(hFile);
end;procedure TForm1.Button1Click(Sender: TObject);
var Mp3Info:TMp3Info;
begin
if OpenDialog1.Execute then
Mp3Info:=GetMp3Info(OpenDialog1.FileName);
with Mp3Info do
begin
if identify='TAG' then
ShowMessage('歌名:'+Title+#13+
'歌手:'+Artist+#13+
'专辑:'+Album+#13+
'年份:'+Year+#13+
'备注:'+Comment);
end;
end;end.
“唉,反正都写了,贴出来算了”谢谢你的代码,如果觉得不方便贴出来的话,可以发到
期待你的回音。。
将那句CreateFile的GENERIC_ALL改成GENERIC_READ就可以了