如何读写mp3的音乐信息???

yagas 2003-10-17 10:19:58
想自己写一个播放器,但……
如何读写mp3的音乐信息???

...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vagerent 2003-10-20
  • 打赏
  • 举报
回复
ok,email已发出
pankun 2003-10-18
  • 打赏
  • 举报
回复
转.

mp3 ID3标签信息

unit ID3Kernel;

interface

type

Tid3v1= record

Tag: array[0..2] of char; //00..02 , ='TAG'

Title:array[0..$1d] of char; //03..20

Artist:array[0..$1d] of char; //21..3e

Album:array[0..$1d] of char; //3f..5c

Year:array[0..3] of char; //5d..60

Comment:array[0..$1c] of char; //61..7d

Track:byte; //7e

Genre:byte; //7f

end;

function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;

function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;

function DeleteID3v1(strFile:string):integer;

implementation

function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;

var

f1:file of byte;

bytAll: array [0..$7f] of byte;

i: integer;

begin

result:=1;

if strFile='' then exit;

AssignFile(f1,strFile);

FileMode:=0;

Reset(f1);

if FileSize(f1)<=$80 then exit;

Seek(f1, FileSize(f1)-$80);

for i:=0 to $7f do Read(f1,bytAll[i]);

if (bytAll[0]<>ord('T')) and (bytAll[1]<>ord('A'))

and (bytAll[2]<>ord('G')) then exit; // no 'TAG' found

Move(bytAll,pid3v1,$80);

CloseFile(f1);

result:=0;

end;

function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;

var

f1:file of byte;

bytAll: array [0..$7f] of byte;

i: integer;

begin

result:=1;

AssignFile(f1,strFile);

FileMode:=2;

Reset(f1);

if FileSize(f1)<=$80 then exit;

Seek(f1, FileSize(f1)-$80);

for i:=0 to $2 do Read(f1,bytAll[i]); // test if 'TAG' exists

if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))

and (bytAll[2]=ord('G'))

then Seek(f1,FileSize(f1)-$80)

else Seek(f1,FileSize(f1));

Move(pid3v1,bytAll,$80);

for i:=0 to $7f do Write(f1,bytAll[i]);

CloseFile(f1);

result:=0;

end;

function DeleteID3v1(strFile:string):integer;

var

f1:file of byte;

bytAll: array [0..$7f] of byte;

i: integer;

begin

Result:=1;

AssignFile(f1,strFile);

FileMode:=2;

Reset(f1);

if FileSize(f1)<=$80 then exit;

Seek(f1, FileSize(f1)-$80);

for i:=0 to $2 do Read(f1,bytAll[i]); // test if 'TAG' exists

if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))

and (bytAll[2]=ord('G'))

then begin

Seek(f1,FileSize(f1)-$80);

Truncate(f1)

end;

CloseFile(f1);

Result:=0;

end;

end.

***********************************************

{

Byte 1-3 = ID 'TAG'

Byte 4-33 = Titel / Title

Byte 34-63 = Artist

Byte 64-93 = Album

Byte 94-97 = Jahr / Year

Byte 98-127 = Kommentar / Comment

Byte 128 = Genre

}

 

type

TID3Tag = record

ID: string[3];

Titel: string[30];

Artist: string[30];

Album: string[30];

Year: string[4];

Comment: string[30];

Genre: Byte;

end;

const

Genres : array[0..146] of string =

('Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge',

'Hip- Hop','Jazz','Metal','New Age','Oldies','Other','Pop','R&B',

'Rap','Reggae','Rock','Techno','Industrial','Alternative','Ska',

'Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient',

'Trip-Hop','Vocal','Jazz+Funk','Fusion','Trance','Classical',

'Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise',

'Alternative Rock','Bass','Punk','Space','Meditative','Instrumental Pop',

'Instrumental Rock','Ethnic','Gothic','Darkwave','Techno-Industrial','Electronic',

'Pop-Folk','Eurodance','Dream','Southern Rock','Comedy','Cult','Gangsta',

'Top 40','Christian Rap','Pop/Funk','Jungle','Native US','Cabaret','New Wave',

'Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk',

'Acid Jazz','Polka','Retro','Musical','Rock & Roll','Hard Rock','Folk',

'Folk-Rock','National Folk','Swing','Fast Fusion','Bebob','Latin','Revival',

'Celtic','Bluegrass','Avantgarde','Gothic Rock','Progressive Rock',

'Psychedelic Rock','Symphonic Rock','Slow Rock','Big Band','Chorus',

'Easy Listening','Acoustic','Humour','Speech','Chanson','Opera',

'Chamber Music','Sonata','Symphony','Booty Bass','Primus','Porn Groove',

'Satire','Slow Jam','Club','Tango','Samba','Folklore','Ballad',

'Power Ballad','Rhytmic Soul','Freestyle','Duet','Punk Rock','Drum Solo',

'Acapella','Euro-House','Dance Hall','Goa','Drum & Bass','Club-House',

'Hardcore','Terror','Indie','BritPop','Negerpunk','Polsk Punk','Beat',

'Christian Gangsta','Heavy Metal','Black Metal','Crossover','Contemporary C',

'Christian Rock','Merengue','Salsa','Thrash Metal','Anime','JPop','SynthPop');

 

var

Form1: TForm1;

implementation

{$R *.dfm}

function readID3Tag(FileName: string): TID3Tag;

var

FS: TFileStream;

Buffer: array [1..128] of Char;

begin

FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

try

FS.Seek(-128, soFromEnd);

FS.Read(Buffer, 128);

with Result do

begin

ID := Copy(Buffer, 1, 3);

Titel := Copy(Buffer, 4, 30);

Artist := Copy(Buffer, 34, 30);

Album := Copy(Buffer, 64, 30);

Year := Copy(Buffer, 94, 4);

Comment := Copy(Buffer, 98, 30);

Genre := Ord(Buffer[128]);

end;

finally

FS.Free;

end;

end;

procedure TfrmMain.Button1Click(Sender: TObject);

begin

if OpenDialog1.Execute then

begin

with readID3Tag(OpenDialog1.FileName) do

begin

LlbID.Caption := 'ID: ' + ID;

LlbTitel.Caption := 'Titel: ' + Titel;

LlbArtist.Caption := 'Artist: ' + Artist;

LlbAlbum.Caption := 'Album: ' + Album;

LlbYear.Caption := 'Year: ' + Year;

LlbComment.Caption := 'Comment: ' + Comment;

if (Genre >= 0) and (Genre <=146) then

LlbGenre.Caption := 'Genre: ' + Genres[Genre]

else

LlbGenre.Caption := 'N/A';

end;

end;

end;
yagas 2003-10-18
  • 打赏
  • 举报
回复
yagas@163.com
vagerent 2003-10-18
  • 打赏
  • 举报
回复
我以前做过一个mp3播放器,留下email我给你寄过去阿。里面很全,拖拽播放,循环,列表,保存m3u,都有了。特别适合新手学习!先给你读取mp3信息的代码:


procedure TForm1.FileListBox1Click(Sender: TObject);
var mymp3:TID3Tag; mp3file:tfilestream;
begin
listbox1.Clear;
mp3file:=tfilestream.Create(self.FileListBox1.FileName,fmOpenRead);
try
mp3file.position:=mp3file.size-128; // 跳到id3-tag
mp3file.Read(mymp3,SizeOf(mymp3));
listbox1.Items.Add('歌曲名称:'+mymp3.Title);
listbox1.Items.Add('艺术家:'+mymp3.Artist);
listbox1.Items.Add('专辑:'+mymp3.Album);
listbox1.Items.Add('出版日期:'+mymp3.Year);
listbox1.Items.Add('评论:'+mymp3.Comment);
edit1.Text:= mymp3.Title;
finally
mp3file.Free;
end;
end;


别忘了加上
type
TID3Tag = packed record // 128 字节
TAGID: array[0..2] of char; // 3 字节: 必须是TAG
Title: array[0..29] of char; // 30 字节: 歌曲标题
Artist: array[0..29] of char; // 30 字节: 歌曲的艺术家
Album: array[0..29] of char; // 30 字节: 歌曲专辑
Year: array[0..3] of char; // 4 字节: 出版年
Comment: array[0..29] of char; // 30 字节: 评论
Genre: byte; // 1 字节: 种类标识

//放在 TForm1 = class(TForm)之前
yagas 2003-10-18
  • 打赏
  • 举报
回复
老大
能加一上注解吗??
小弟新手呀,看不太明白!!

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧