AS3 载入MP3 ID3 乱码解决办法

大陆大部份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里br />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时,可以另加代码,用文件名代替..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值