---
title: "AS3 载入MP3 ID3 乱码解决办法"
author: "wgcode"
date: "2010-12-23T13:50:15Z"
tags: []
categories: ["编程语言"]
slug: "as3-mp3-id3"
draft: false
lastmod: "2010-12-23T13:50:15Z"
id: 850146
url: "https://tts.iteye.com/blog/850146"
---

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