制作mp3 的标签(ID3v1)修改程序,将标签定义成一个结构,读正常,赋值正常,但是当写入mp3文件的时候却不正常,察看mp3文件,被写入的结构成员的值加上了一些怪异符号,结果无法准确定位读取信息。
比如: 一般mp3 的id3v1 设置后 是:
TAGBoring Day 周华健 一起吃苦的幸福 2003滚石唱片
但是写入文件后变成:
?TAG Boring Day ?周华健 ???一起吃苦的幸福 ?2003?滚石唱片
(?表示奇怪字符,数目固定),
很是郁闷,请大虾指教
比如: 一般mp3 的id3v1 设置后 是:
TAGBoring Day 周华健 一起吃苦的幸福 2003滚石唱片
但是写入文件后变成:
?TAG Boring Day ?周华健 ???一起吃苦的幸福 ?2003?滚石唱片
(?表示奇怪字符,数目固定),
很是郁闷,请大虾指教
大哥
?是怪字符,我打不出来的替代,对不起,就是些ascII码,
读文件时正常,我看了,mp3的id3v1要定位才能准确读出
我详细说明:
定义结构: type
id3=record
tag:string[3];
title:string[30];
artist:string[30];
album:string[30];
year:string[4];
genre:char;
comment:string[30];
总长度是128,定位读正常,定位写不对。我用的是blockread and blockwrite
tag:string;
title:string;
artist:string;
album:string;
year:string;
genre:char;
comment:string;
id3= packed record
tag:string[3];
title:string[30];
artist:string[30];
album:string[30];
year:string[4];
genre:char;
comment:string[30];如果不加packed,會自動擴展長度以便優化,用sizeof(id3)一試便知
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;type
TID3Tag = record
Tag: array[0..2] of Char;
Songname: array[0..29] of Char;
Artist: array[0..29] of Char;
Album: array[0..29] of Char;
Year: array[0..3] of Char;
Comment: array[0..29] of Char;
Genre: Byte;
end;type
TID3Info = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Name: TLabel;
Artist: TLabel;
Album: TLabel;
Year: TLabel;
Comment: TLabel;
Remove: TBitBtn;
Update: TBitBtn;
OK: TBitBtn;
procedure RemoveClick(Sender: TObject);
procedure UpdateClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure OKClick(Sender: TObject);
private
{ Private declarations }
FN : String;
public
{ Public declarations }
function ReadID3Tag(FileName: string): TID3Tag;
function WriteID3Tag(FileName: string; ID3InfoTag: TID3Tag): Boolean;
function RemoveID3Tag(FileName: string): Boolean;
function ID3TagsAreEqual(ID3Tag1, ID3Tag2: TID3Tag): Boolean;
end;
var
ID3Info: TID3Info;
id3tag: tid3tag;implementationuses MP_List_Unit;{$R *.DFM}procedure TID3Info.RemoveClick(Sender: TObject);
begin
if FileExists(FN) then
if RemoveID3Tag(FN) then
begin
MessageDlg('Remove ok!', mtInformation, [mbOK], 0);
edit1.Text:='';
edit2.Text:='';
edit3.Text:='';
edit4.Text:='';
edit5.Text:='';
end else MessageDlg('Remove failed!', mtError, [mbOK], 0);
end;function TID3Info.ID3TagsAreEqual(ID3Tag1, ID3Tag2: TID3Tag): Boolean;
begin
with ID3Tag1 do
if (string(ID3Tag2.Tag) <> string(Tag)) or
(string(ID3Tag2.Songname) <> string(SongName)) or
(string(ID3Tag2.Artist) <> string(Artist)) or
(string(ID3Tag2.Album) <> string(Album)) or
(string(ID3Tag2.Year) <> string(Year)) or
(string(ID3Tag2.Comment) <> string(Comment)) or
(ID3Tag2.Genre <> Genre) then Result := False
else Result := True;
end;function TID3Info.ReadID3Tag(FileName: string): TID3Tag;
var
F: file;
begin
{$I-}
AssignFile(F, FileName);
Reset(F, 1);
Seek(F, Filesize(F) - SizeOf(TID3Tag));
Result.Tag[1] := 'N';
BlockRead(F, Result, SizeOf(TID3Tag));
CloseFile(F);
{$I+}
end;function TID3Info.RemoveID3Tag(FileName: string): Boolean;
var
FileHandle: integer;
attr:Integer;
begin
{$I-}
attr:=FileGetAttr(FileName);
FileSetAttr(FileName,faArchive);
FileHandle := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
if FileHandle > 0 then
if ID3Tag.Tag = 'TAG' then
begin
FileSeek(FileHandle, 0 - SizeOf(TID3Tag), 2);
SetEndOfFile(FileHandle);
end;
FileClose(FileHandle);
result := FileHandle <> 0;
FileSetAttr(FileName,attr);
{$I+}
end;function TID3Info.WriteID3Tag(FileName: string; ID3InfoTag: TID3Tag): Boolean;
var
F: file;
X: Integer;
attr:Integer;
begin
{$I-}
attr:=FileGetAttr(FileName);
FileSetAttr(FileName,faArchive);
AssignFile(F, FileName);
Reset(F, 1);
X:=0;
if ReadID3Tag(FileName).Tag <> 'TAG' then Seek(F, FileSize(F))
else Seek(F, FileSize(F) - SizeOf(TID3Tag));
BlockWrite(F, ID3Tag, SizeOf(TID3Tag), X);
Result := (X <> 0);
CloseFile(F);
FileSetAttr(FileName,attr);
{$I+}
end;procedure TID3Info.UpdateClick(Sender: TObject);
var s: string;
begin
if FileExists(fn) then
with ID3Tag do
begin
Tag := 'TAG';
S := edit1.text;
if length(S) > 30 then setlength(S, 30);
StrPCopy(Songname, S);
S := edit2.text;
if length(S) > 30 then setlength(S, 30);
StrPCopy(Artist, S);
S := edit3.text;
if length(S) > 30 then setlength(S, 30);
StrPCopy(Album, S);
S := edit4.text;
if length(S) > 4 then setlength(S, 4);
StrPCopy(Year, S);
S := edit5.text;
if length(S) > 30 then setlength(S, 30);
StrPCopy(Comment, S);
if WriteID3Tag(fn, ID3Tag)
then MessageDlg('Update ok!', mtInformation, [mbOK], 0)
else begin
MessageDlg('Update failed!', mtError, [mbOK], 0);
FormShow(nil);
end;
end;
end;procedure TID3Info.FormShow(Sender: TObject);
begin
FN := SongListForm.GetSelectedFileName;
if FileExists(FN)
then ID3Tag:=ReadID3Tag(FN)
else begin
MessageDlg('File cannot be found!', mtError, [mbOK], 0);
PostMessage(Handle, WM_Close, 0, 0);
Exit;
end;
if (ID3Tag.Tag = 'TAG') and (trim(ID3Tag.songname) <> '') then
begin
//Display the Songname etc.. in the Label
edit1.Text := Trim(ID3Tag.songname);
edit2.Text := Trim(ID3Tag.Artist);
edit3.Text := Trim(ID3Tag.Album);
edit4.Text := Trim(ID3Tag.Year);
edit5.Text := Trim(ID3Tag.Comment);
end else
begin
edit1.Text := ExtractFileName(FN);
edit2.Text := '';
edit3.Text := '';
edit4.Text := '';
edit5.Text := '';
end;
end;procedure TID3Info.OKClick(Sender: TObject);
begin
Close;
end;end.
id3=record
tag:string[3];
title:string[30];
artist:string[30];
album:string[30];
year:string[4];
genre:char;
comment:string[30];
end;
老兄,你的结构大小为134,你需要这样定义:
type
id3= record
tag: array[0..2] of char;
title: array[0..29] of char;
artist:array[0..29] of char;
album:array[0..29] of char;
year:array[0..3] of char;
genre:char;
comment:array[0..29] of char;
end;//保你成功