看贴出来读取标签的代码:unit ID3v2;interfaceuses
Classes, SysUtils,Dialogs;type
Tarraychar = array of char; TID3v2 = class(TObject)
private
{ Private declarations }
FExists: Boolean;
FTitle: String;
FArtist: String;
FAlbum: String;
FYear: String;
FComment: String;
FTrack: string;
FGenre: string;
procedure FSetTitle(const NewTitle: String);
procedure FSetArtist(const NewArtist: String);
procedure FSetAlbum(const NewAlbum: String);
procedure FSetYear(const NewYear: String);
procedure FSetComment(const NewComment: String);
procedure FSetTrack(const NewTrack: string);
procedure FSetGenre(const NewGenre: string);
public
{ Public declarations }
constructor Create; { Create object }
procedure ResetData; { Reset all data }
function ReadFromFile(const FileName: string): Boolean; { Load tag }
//function RemoveFromFile(const FileName: string): Boolean; { Delete tag }
//function SaveToFile(const FileName: string): Boolean; { Save tag }
property Exists: Boolean read FExists; { True if tag found }
//property VersionID: Byte read FVersionID; { Version code }
property Title: String read FTitle write FSetTitle; { Song title }
property Artist: String read FArtist write FSetArtist; { Artist name }
property Album: String read FAlbum write FSetAlbum; { Album name }
property Year: String read FYear write FSetYear; { Year }
property Comment: String read FComment write FSetComment; { Comment }
property Track: string read FTrack write FSetTrack; { Track number }
property Genre: string read FGenre write FSetGenre; { Genre name }
end;
implementationtype
{ Real structure of ID3v2 tag }
TagHeaderRecord = record
Header: array [1..3] of Char; { Tag header - must be "ID3" }
Ver: array [1..1] of Char;
Revision: array [1..1] of Char;
Flag: array [1..1] of char;
Size: array [1..4] of byte; { Genre data }
end; FrameHeaderRecord = record
Header: array [1..4] of char;
Size: array [1..4] of byte;
Flag: array [1..2] of char;
end;
constructor TID3v2.Create;
begin
inherited;
ResetData;
end;procedure TID3v2.ResetData;
begin
FExists := false;
FTitle := '';
FArtist := '';
FAlbum := '';
FYear := '';
FComment := '';
FTrack := '0';
FGenre := '';
end;function Unicodetostr(var chararray:Tarraychar):string;
var
i:integer;
begin
Result:='';
if length(chararray)> 3 then
begin
if (chararray[1]= #255) and (chararray[2]=#254) then
begin
for i:= 3 to high(chararray) do
begin
if i mod 2 =0 then
Result := Result + widechar(byte(chararray[i])*256 + byte(chararray[i-1]));
end;
end;
if (chararray[1]=#254) and (chararray[2]=#255) then
begin
for i:= 3 to high(chararray) do
begin
if i mod 2 =0 then
Result := Result + widechar(byte(chararray[i-1])*256 + byte(chararray[i]));
end;
end;
end;
end;function UTF8tostr(var chararray:Tarraychar):string;
var
i:integer;
begin
Result:= '';
if length(chararray) > 1 then
begin
for i:= 1 to high(chararray) do
chararray[i-1]:=chararray[i];
chararray[high(chararray)]:=#0;
Result:=Utf8ToAnsi(string(chararray));
end;
end;function TID3v2.ReadFromFile(const FileName: string): Boolean;
var
sourcefile:file;
TagHeaderData: TagHeaderRecord;
FrameHeaderData: FrameHeaderRecord;
TagSize:integer;
FrameSize:integer;
Tmparray:Tarraychar;
begin
Result := true;
ResetData;
try
{ Set read-access and open file }
AssignFile(SourceFile, FileName);
FileMode := 0;
Reset(SourceFile, 1);
{ Read tag }
//Seek(SourceFile, FileSize(SourceFile) - 128);
BlockRead(SourceFile, TagHeaderData, 10);
if TagHeaderData.Header = 'ID3' then
begin
FExists := True;
TagHeaderData.Size[1] := TagHeaderData.Size[1] and 127;
TagHeaderData.Size[2] := TagHeaderData.Size[2] and 127;
TagHeaderData.Size[3] := TagHeaderData.Size[3] and 127;
TagHeaderData.Size[4] := TagHeaderData.Size[4] and 127;
Tagsize := TagHeaderData.Size[1]*2097152 + TagHeaderData.Size[2]*1024
+ TagHeaderData.Size[3]*128 + TagHeaderData.Size[4];
While filepos(Sourcefile) < Tagsize do
begin
BlockRead(SourceFile, FrameHeaderData, 10);
FrameSize := FrameHeaderData.Size[1]*2097152 + FrameHeaderData.Size[2]*1024
+ FrameHeaderData.Size[3]*128 + FrameHeaderData.Size[4];
setlength(tmparray,FrameSize);
if FrameHeaderData.Header = 'TIT2' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FTitle:=Trim(string(tmparray));
1:FTitle:=Unicodetostr(tmparray);
3:FTitle:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TPE1' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FArtist:=Trim(string(tmparray));
1:FArtist:=Unicodetostr(tmparray);
3:FArtist:=UTF8toStr(tmparray);
end;
continue;
end;
{if FrameHeaderData.Header = 'TALB' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0: FAlbum:=Trim(string(tmparray));
1: FAlbum:=Unicodetostr(tmparray);
3: FAlbum:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TYER' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FYear:=Trim(string(tmparray));
1:FYear:=Unicodetostr(tmparray);
3:FYear:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'COMM' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:begin
Fillchar(tmparray[0],5,ord(' '));
FComment:=Trim(string(tmparray));
end;
1:FComment:=Unicodetostr(tmparray);
3:FComment:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TCON' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FGenre:=Trim(string(tmparray));
1:FGenre:=Unicodetostr(tmparray);
3:FGenre:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TRCK' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FTrack:=Trim(string(tmparray));
1:FTrack:=Unicodetostr(tmparray);
3:FTrack:=UTF8toStr(tmparray);
end;
continue;
end;}
BlockRead(SourceFile, tmparray[0], FrameSize);
end;
end;
CloseFile(SourceFile);
except
{ Error }
Result := false;
ResetData;
CloseFile(sourcefile);//防止无法移动文件、
end;
end;procedure TID3v2.FSetTitle(const NewTitle: String);
begin
FTitle := Trim(NewTitle);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetArtist(const NewArtist: String);
begin
FArtist := Trim(NewArtist);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetAlbum(const NewAlbum: String);
begin
FAlbum := Trim(NewAlbum);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetYear(const NewYear: String);
begin
FYear := Trim(NewYear);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetComment(const NewComment: String);
begin
FComment := Trim(NewComment);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetTrack(const NewTrack: string);
begin
FTrack := NewTrack;
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetGenre(const NewGenre: string);
begin
FGenre := NewGenre;
end;end.
Classes, SysUtils,Dialogs;type
Tarraychar = array of char; TID3v2 = class(TObject)
private
{ Private declarations }
FExists: Boolean;
FTitle: String;
FArtist: String;
FAlbum: String;
FYear: String;
FComment: String;
FTrack: string;
FGenre: string;
procedure FSetTitle(const NewTitle: String);
procedure FSetArtist(const NewArtist: String);
procedure FSetAlbum(const NewAlbum: String);
procedure FSetYear(const NewYear: String);
procedure FSetComment(const NewComment: String);
procedure FSetTrack(const NewTrack: string);
procedure FSetGenre(const NewGenre: string);
public
{ Public declarations }
constructor Create; { Create object }
procedure ResetData; { Reset all data }
function ReadFromFile(const FileName: string): Boolean; { Load tag }
//function RemoveFromFile(const FileName: string): Boolean; { Delete tag }
//function SaveToFile(const FileName: string): Boolean; { Save tag }
property Exists: Boolean read FExists; { True if tag found }
//property VersionID: Byte read FVersionID; { Version code }
property Title: String read FTitle write FSetTitle; { Song title }
property Artist: String read FArtist write FSetArtist; { Artist name }
property Album: String read FAlbum write FSetAlbum; { Album name }
property Year: String read FYear write FSetYear; { Year }
property Comment: String read FComment write FSetComment; { Comment }
property Track: string read FTrack write FSetTrack; { Track number }
property Genre: string read FGenre write FSetGenre; { Genre name }
end;
implementationtype
{ Real structure of ID3v2 tag }
TagHeaderRecord = record
Header: array [1..3] of Char; { Tag header - must be "ID3" }
Ver: array [1..1] of Char;
Revision: array [1..1] of Char;
Flag: array [1..1] of char;
Size: array [1..4] of byte; { Genre data }
end; FrameHeaderRecord = record
Header: array [1..4] of char;
Size: array [1..4] of byte;
Flag: array [1..2] of char;
end;
constructor TID3v2.Create;
begin
inherited;
ResetData;
end;procedure TID3v2.ResetData;
begin
FExists := false;
FTitle := '';
FArtist := '';
FAlbum := '';
FYear := '';
FComment := '';
FTrack := '0';
FGenre := '';
end;function Unicodetostr(var chararray:Tarraychar):string;
var
i:integer;
begin
Result:='';
if length(chararray)> 3 then
begin
if (chararray[1]= #255) and (chararray[2]=#254) then
begin
for i:= 3 to high(chararray) do
begin
if i mod 2 =0 then
Result := Result + widechar(byte(chararray[i])*256 + byte(chararray[i-1]));
end;
end;
if (chararray[1]=#254) and (chararray[2]=#255) then
begin
for i:= 3 to high(chararray) do
begin
if i mod 2 =0 then
Result := Result + widechar(byte(chararray[i-1])*256 + byte(chararray[i]));
end;
end;
end;
end;function UTF8tostr(var chararray:Tarraychar):string;
var
i:integer;
begin
Result:= '';
if length(chararray) > 1 then
begin
for i:= 1 to high(chararray) do
chararray[i-1]:=chararray[i];
chararray[high(chararray)]:=#0;
Result:=Utf8ToAnsi(string(chararray));
end;
end;function TID3v2.ReadFromFile(const FileName: string): Boolean;
var
sourcefile:file;
TagHeaderData: TagHeaderRecord;
FrameHeaderData: FrameHeaderRecord;
TagSize:integer;
FrameSize:integer;
Tmparray:Tarraychar;
begin
Result := true;
ResetData;
try
{ Set read-access and open file }
AssignFile(SourceFile, FileName);
FileMode := 0;
Reset(SourceFile, 1);
{ Read tag }
//Seek(SourceFile, FileSize(SourceFile) - 128);
BlockRead(SourceFile, TagHeaderData, 10);
if TagHeaderData.Header = 'ID3' then
begin
FExists := True;
TagHeaderData.Size[1] := TagHeaderData.Size[1] and 127;
TagHeaderData.Size[2] := TagHeaderData.Size[2] and 127;
TagHeaderData.Size[3] := TagHeaderData.Size[3] and 127;
TagHeaderData.Size[4] := TagHeaderData.Size[4] and 127;
Tagsize := TagHeaderData.Size[1]*2097152 + TagHeaderData.Size[2]*1024
+ TagHeaderData.Size[3]*128 + TagHeaderData.Size[4];
While filepos(Sourcefile) < Tagsize do
begin
BlockRead(SourceFile, FrameHeaderData, 10);
FrameSize := FrameHeaderData.Size[1]*2097152 + FrameHeaderData.Size[2]*1024
+ FrameHeaderData.Size[3]*128 + FrameHeaderData.Size[4];
setlength(tmparray,FrameSize);
if FrameHeaderData.Header = 'TIT2' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FTitle:=Trim(string(tmparray));
1:FTitle:=Unicodetostr(tmparray);
3:FTitle:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TPE1' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FArtist:=Trim(string(tmparray));
1:FArtist:=Unicodetostr(tmparray);
3:FArtist:=UTF8toStr(tmparray);
end;
continue;
end;
{if FrameHeaderData.Header = 'TALB' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0: FAlbum:=Trim(string(tmparray));
1: FAlbum:=Unicodetostr(tmparray);
3: FAlbum:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TYER' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FYear:=Trim(string(tmparray));
1:FYear:=Unicodetostr(tmparray);
3:FYear:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'COMM' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:begin
Fillchar(tmparray[0],5,ord(' '));
FComment:=Trim(string(tmparray));
end;
1:FComment:=Unicodetostr(tmparray);
3:FComment:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TCON' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FGenre:=Trim(string(tmparray));
1:FGenre:=Unicodetostr(tmparray);
3:FGenre:=UTF8toStr(tmparray);
end;
continue;
end;
if FrameHeaderData.Header = 'TRCK' then
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FTrack:=Trim(string(tmparray));
1:FTrack:=Unicodetostr(tmparray);
3:FTrack:=UTF8toStr(tmparray);
end;
continue;
end;}
BlockRead(SourceFile, tmparray[0], FrameSize);
end;
end;
CloseFile(SourceFile);
except
{ Error }
Result := false;
ResetData;
CloseFile(sourcefile);//防止无法移动文件、
end;
end;procedure TID3v2.FSetTitle(const NewTitle: String);
begin
FTitle := Trim(NewTitle);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetArtist(const NewArtist: String);
begin
FArtist := Trim(NewArtist);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetAlbum(const NewAlbum: String);
begin
FAlbum := Trim(NewAlbum);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetYear(const NewYear: String);
begin
FYear := Trim(NewYear);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetComment(const NewComment: String);
begin
FComment := Trim(NewComment);
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetTrack(const NewTrack: string);
begin
FTrack := NewTrack;
end;{ --------------------------------------------------------------------------- }procedure TID3v2.FSetGenre(const NewGenre: string);
begin
FGenre := NewGenre;
end;end.
解决方案 »
- 求救
- 高分求救,进则有分,答对另开帖给分!!!
- 【ListView的几个问题】请教。
- 我是刚学Delphi的,遇到一个问题,请帮忙相告!
- 本人应届生,想找DELPHI的工作,全职,各位有消息的话可否帮忙?
- 求救!!如何将窗体上的控件保存为文件流的形式,以及如何将次文件打开?
- 如何制作多语言啊 ,delphi有相应的第三方控件吗?
- Delohi 中关于快捷菜单的调用?
- 我是delphi的菜鸟啊,希望大家多多关照!!!!公开你们的QQ吧!!
- 为什么DELPHI5的TClientdataset的ApplyUpdates()问题?
- 我的MainMenu1不见了??
- 《Windows程序设计》第四章例子
1.申明全局变量
ID3v2:TID3v2;
以及一个类型
Type
IDTips = Record
Title,Artist,Album,Year,Comment,Track,Genre:string;
End;
2.窗体create事件
ID3v2:=TID3v2.Create;
3.读取信息(我先获得一个需要读取的文件列表)Var
FileList:TStringList;
MusicTips:IDTips;
i:Integer;
begin
//获得文件列表的省略
For i:= 0 To FileList.Count - 1 Do
Begin
MusicTips:=Id3v2R(FileList[i]);//获得标签信息
Application.ProcessMessages;
//后面的省略、、
end;
end;//**********************Id3v2R**********函数
Function Id3v2R(Path:string):IDTips;
Begin ID3v2.ReadFromFile(Path);
With Result Do
Begin
Title:=ID3v2.Title;
Artist:=ID3v2.Artist;
end;
end;
我看了很久、都没有找出为什么会这样、希望得到各位的帮助
嗯、、尽管分数不多、要的来接吧、、下午就结贴了
begin
BlockRead(SourceFile, tmparray[0], FrameSize);
case byte(tmparray[0]) of
0:FTitle:=Trim(string(tmparray));
1:FTitle:=Unicodetostr(tmparray);
3:FTitle:=UTF8toStr(tmparray);
end;
continue;
end;
要把 BlockRead(SourceFile, tmparray[0], FrameSize);
提前到判断前面,这样直接查找下一个frame,你放在里面,导致每一个字节都要查找一遍,所以效率低