flash如果mp3的id3标签使用gb2312编码,那么在flash脚本输出时是乱码的
代码1
var s:sound=new sound(this);
s.loadsound("dxh.mp3",false);
s.onid3=function(){
trace(this.id3.songname);
}
输出结果是:
¶¡ïã»
dxh.mp3的id3v1的标签正确应该是songname="丁香花",看来flash在转码上出现了问题。我们来看看songname这个字符串中倒底是什么?
代码2:var s:sound=new sound(this);
s.loadsound("dxh.mp3",false);
s.onid3=function(){
var songname:string=this.id3.songname;
for(var i=0;i<songname.length;i++){
trace(songname.charcodeat(i));
}
}
输出结果是:
182
161
207
227
187
168
我们使用计算器转换成16进制就是"b6 a1 cf e3 bb a8";
正好是"丁香花"的gb2312编码,我们还是用flash来试试
system.usecodepage=true;
trace(unescape("%b6%a1%cf%e3%bb%a8"));
输出结果是:
丁香花
那么为什么代码1出现乱码现象,是因为flash将gb2312当作了utf-8来解释,我们再来测试一下:
代码3:
var s:sound=new sound(this);
s.loadsound("dxh.mp3",false);
s.onid3=function(){
var songname:string=this.id3.songname;
trace(escape(songname));
}
结果是:
%3f%3f%3f%3f%3f%a1%a7
问题的原因我们找到了,只要将gb2312转换成utf-8编码就能显示正常了,可是如果转换呢,大家注意看代码2,我再测试一下想法
代码4:
system.usecodepage=true;
var gb:string=unescape("%b6%a1%cf%e3%bb%a8");
system.usecodepage=false;
trace(gb);
trace(escape(gb));
输出结果:
丁香花
%e4%b8%81%e9%a6%99%e8%8a%b1
第二行就是“丁香花”的utf-8编码,说明已经转换成功了,我们来具体实现这个过程
class lm.utils.lutf {
public function toutf(source:string):string{
var target:string="";
for(var i=0;i<source.length;i++){
target+=this.codetohex(source.charcodeat(i));
}
system.usecodepage=true;
target=unescape(target);
system.usecodepage=false;
return target;
}
private function codetohex(code:number):string{
var low:number=code%16;
var high:number=(code-low)/16;
return "%"+hex(high)+hex(low);
}
private function hex(code:number):string{
switch(code){
case 10:
return "a";
break;
case 11:
return "b";
break;
case 12:
return "c";
break;
case 13:
return "d";
break;
case 14:
return "e";
break;
case 15:
return "f";
break;
default:
return string(code);
break;
}
}
}
我们再来测试一下
import lm.utils.lutf;
var u=new lutf();
var s:sound=new sound(this);
s.loadsound("dxh.mp3",false);
s.onid3=function(){
var songname:string=_root.u.toutf(this.id3.songname);
trace(songname);
}
输出结果:
丁香花
到此为此我们已经解决了这个乱码问题,使用这个技巧也可以解决其他的乱码问题!
出处:闪吧
责任编辑:qhwa
◎进入论坛flash专栏版块参加讨论
| ||
| flash 酷站猎手 中文id3乱码问题mx解决方案 flash常用效果-移动模糊效果 flash mx 2005 制作环境预览 macromedia flashcast |
| |
| 用as2解决中文id3的乱码 |
| ||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||
| |
|
>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器