大猴子的花果山
-
AS3 载入MP3 ID3 乱码解决办法 - [ActionScript3.0]
2007-12-28
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://25swf.blogbus.com/logs/12867189.html
大陆大部份MP3 的ID3用的字符编码都是 GBK/GB2312 的。在AS2 中,获取ID3时使用 usedCodePage 便可解决问题,但在AS3中 即使使用了 usedCodePage,Sound 在载入mp3读取ID3信息使,仍使用的是 UTF8 的编码,这使得大量MP3 读出的ID3是乱码,无法正常显是,这是很不爽的事。通过以下代码,可以使得被误以为是UTF8编码的ID3信息从乱码变成可读.(以下以id3.songName为例)
if(this.id3.songName != null)
{
//首先,在ID3 事件里。把乱码按utf-8的编码写进ByteArray里
var myByteArray : ByteArray = new ByteArray();
myByteArray.writeUTFBytes(this.id3.songName);
//还原成可读的字符
trace(gb2312ToUtf8(myByteArray));
}
/**
* @author 25swf
* @blog http://www.25swf.com/
* @param myByteArray 被当成UTF8读取的gb2312字节数组
*/
private function gb2312ToUtf8(myByteArray : ByteArray) : String {
var tempByteArray : ByteArray = new ByteArray();
for(var a:int = 1;a {
if(myByteArray[a-1] == 194)
tempByteArray.writeByte(myByteArray[a]);
else if(myByteArray[a-1] == 195)
tempByteArray.writeByte(myByteArray[a] + 64);
else
{//是英文数字
tempByteArray.writeByte(myByteArray[a-1]);
tempByteArray.writeByte(myByteArray[a]);
}
}
//重设游标
tempByteArray.position = 0;
return tempByteArray.readMultiByte(tempByteArray.bytesAvailable, "cn-bg");
}
另外,如果songName为null时,可以另加代码,用文件名代替..随机文章:
用AS3载入BMP图像 2008-01-15AS3的一个提示类 2008-01-10AS3 ICellRenderer 的一个示例 2008-01-10AS3 获取loader载入的SWF的帧数 2007-12-01AS2 socket 的安全策略 与 AS3 socket 的安全策略 2007-11-21
收藏到:Del.icio.us

