vb.net获取mp3的歌曲名和其他属性

vb.net获取mp3的歌曲名和其他属性,第1张

从MP3中提取歌曲信息

一首MP3歌曲除了音乐信息外,还包含了如歌名、演唱者等信息,当我们用winamp软件听音乐时,播放清单就自动将这些信息读出来。大部分人都喜欢从网上下载音乐,但下载下来的MP3文件名都是文件上传系统自动取名的,和歌曲本身根本不相符,所以,给用户带来了很大的麻烦。但是,懒人有懒人的做法,我们何不自己写一个程序,将歌曲信息自动读出来并为MP3文件自动更名呢?

下面以C#为工具,把开发过程写出来。

一首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;//保留位,一个字节

}

所以,我们只要把MP3文件的最后128个字节分段读出来并保存到该结构里就可以了。函数定义如下:

///

/// 获取MP3文件最后128个字节

///

/// 文件名

/// 返回字节数组

private byte[] getLast128(string FileName)

{

FileStream fs = new FileStream(FileName,FileModeOpen,FileAccessRead);

Stream stream = fs;

streamSeek(-128,SeekOriginEnd);

const int seekPos = 128;

int rl = 0;

byte[] Info = new byte[seekPos];

rl = streamRead(Info,0,seekPos);

fsClose();

streamClose();

return Info;

}

再对上面返回的字节数组分段取出,并保存到Mp3Info结构中返回。

///

/// 获取MP3歌曲的相关信息

///

/// 从MP3文件中截取的二进制信息

/// 返回一个Mp3Info结构

private Mp3Info getMp3Info(byte[] Info)

{

Mp3Info mp3Info = new Mp3Info();

string str = null;

int i;

int position = 0;//循环的起始值

int currentIndex = 0;//Info的当前索引值

//获取TAG标识

for(i = currentIndex;i

{

str = str+(char)Info[i];

position++;

}

currentIndex = position;

mp3Infoidentify = str;

//获取歌名

str = null;

byte[] bytTitle = new byte[30];//将歌名部分读到一个单独的数组中

int j = 0;

for(i = currentIndex;i

{

bytTitle[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3InfoTitle = thisbyteToString(bytTitle);

//获取歌手名

str = null;

j = 0;

byte[] bytArtist = new byte[30];//将歌手名部分读到一个单独的数组中

for(i = currentIndex;i

{

bytArtist[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3InfoArtist = thisbyteToString(bytArtist);

//获取唱片名

str = null;

j = 0;

byte[] bytAlbum = new byte[30];//将唱片名部分读到一个单独的数组中

for(i = currentIndex;i

{

bytAlbum[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3InfoAlbum = thisbyteToString(bytAlbum);

//获取年

str = null;

j = 0;

byte[] bytYear = new byte[4];//将年部分读到一个单独的数组中

for(i = currentIndex;i

{

bytYear[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3InfoYear = thisbyteToString(bytYear);

//获取注释

str = null;

j = 0;

byte[] bytComment = new byte[28];//将注释部分读到一个单独的数组中

for(i = currentIndex;i

{

bytComment[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3InfoComment = thisbyteToString(bytComment);

//以下获取保留位

mp3Inforeserved1 = (char)Info[++position];

mp3Inforeserved2 = (char)Info[++position];

mp3Inforeserved3 = (char)Info[++position];

return mp3Info;

}

上面程序用到下面的方法:

///

/// 将字节数组转换成字符串

///

/// 字节数组

/// 返回转换后的字符串

private string byteToString(byte[] b)

{

Encoding enc = EncodingGetEncoding("GB2312");

string str = encGetString(b);

str = strSubstring(0,strIndexOf('\0') >= 0 strIndexOf('\0') : strLength);//去掉无用字符

return str;

}

改名怎么办呢?我们按(演唱者)歌名 的格式对歌曲进行改名,程序如下:

///

/// 更改文件名

///

/// 文件名

///

private bool ReName(string filePath)

{

if(FileExists(filePath))

{

Mp3Info mp3Info = new Mp3Info();

mp3Info = thisgetMp3Info(thisgetLast128(filePath));//读出文件信息

mp3InfoArtist = thisDeleteNotValue(mp3InfoArtist);

mp3InfoTitle = thisDeleteNotValue(mp3InfoTitle);

if(mp3InfoArtistTrim()Length==0)

{

mp3InfoArtist="未命名";

}

if(mp3InfoTitleTrim()Length==0)

{

mp3Infotitle="未知名歌曲";

}

try

{

//更名

FileMove(filePath,filePathSubstring(0,filePathToLower()LastIndexOf("\\"))Trim() + "\\" + "(" + mp3InfoArtistTrim() + ")" +mp3InfoTitleTrim() + "mp3");

return true;

}

catch(Exception)

{

return false;

}

}

else

{

return false;

}

}

是要获取某个文件夹下的全部吗?那样的话需要对文件进行 *** 作,手头没有样例如果是获取特定文件,用这两行:String

E_PATH=EnvironmentgetExternalStorageDirectory()getPath();String

Music_PATH=E_PATH+"/music/xxxmp3";

你还不如把这些信息存在数据库里面 还快些

文件大小

SystemIOFileInfo tm = new SystemIOFileInfo("文件路径");

tmLength;

FileInfo aFile= new FileInfo("C:\\hoantxt"); //实例化一个对

aFileCreationTime; //获取创建时间

aFileExtension; //获取扩展名

aFileLength; //获取大小

其他的百度一下吧 很简单的

就存大小,类型,艺术家这几个东西会造成数据库很大么

大了就用索引查询啊 不用把mp3文件一起存进数据库啊

你一条一条的去读文件列表很耗费资源的

以上就是关于vb.net获取mp3的歌曲名和其他属性全部的内容,包括:vb.net获取mp3的歌曲名和其他属性、如何获取SD卡下面音乐的文件名称、C#:如何获取MP3文件的属性直接显示到网页中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/10060324.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存