[AS3]AS3.0加载外部MP3文件解决ID3信息乱码的问题代码示例
[AS3]AS3.0加载外部MP3文件解决ID3信息乱码的问题代码示例,AS3加载外部MP3文件,需要显示ID3信息时,很多都是乱码。解决的办法是添加一个函数。
AS3加载外部MP3文件,需要显示ID3信息时,很多都是乱码。解决的办法是添加一个函数。
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.media.Sound;
- import flash.media.SoundLoaderContext;
- import flash.net.URLRequest;
- import flash.utils.ByteArray;
- public class LoadSoundExample extends Sprite
- {
- private var _sound:Sound;
- private var dian:Array;
- private var urlReq:URLRequest;
- private var mp3URL:String = "the dawn.mp3";
- private var buffer:SoundLoaderContext;
- public function LoadSoundExample()
- {
- urlReq = new URLRequest(mp3URL);
- _sound = new Sound();
- buffer = new SoundLoaderContext(50000);
- _sound.load(urlReq,buffer);
- _sound.play();
- _sound.addEventListener(Event.ID3, onID3);
- playBtn.addEventListener(MouseEvent.CLICK, clickHandler);
- pauseBtn.addEventListener(MouseEvent.CLICK, clickHandler);
- addEventListener(Event.ENTER_FRAME, onEnterFrames);
- }
- private function onID3(e:Event):void
- {
- _txt.text += EncodeUtf8(_sound.id3.songName+"\n");
- _txt.text += EncodeUtf8(_sound.id3.artist + "\n");
- _txt.text += EncodeUtf8(_sound.id3.album + "\n");
- _txt.text += EncodeUtf8(_sound.id3.year + "\n");
- }
- private function onEnterFrames(e:Event):void
- {
- var barWidth:int = 200;
- var barHeight:int = 5;
- var loaded:int = _sound.bytesLoaded;
- var total:int = _sound.bytesTotal;
- if (total > 0)
- {
- graphics.clear();
- graphics.beginFill(0xffffff);
- graphics.drawRect(10, 10, barWidth, barHeight);
- graphics.endFill();
- var percent:Number = loaded / total;
- graphics.beginFill(0xff0000);
- graphics.drawRect(10, 10, barWidth * percent, barHeight);
- graphics.endFill();
- }
- }
- private function clickHandler(e:MouseEvent):void
- {
- switch (e.target.name)
- {
- case "playBtn":
- _sound.play();
- break;
- case "pauseBtn":
- _sound.close();
- break;
- }
- }
- private function EncodeUtf8(str:String):String
- {
- var oriByteArr:ByteArray = new ByteArray();
- oriByteArr.writeUTFBytes(str);
- var tempByteArr:ByteArray = new ByteArray();
- //trace(str);
- for (var i = 0; i < oriByteArr.length; i++)
- {
- if (oriByteArr[i] == 194)
- {
- tempByteArr.writeByte(oriByteArr[i + 1]);
- i++;
- }
- else if (oriByteArr[i] == 195)
- {
- tempByteArr.writeByte(oriByteArr[i + 1] + 64);
- i++;
- }
- else
- {
- tempByteArr.writeByte(oriByteArr[i]);
- }
- }
- tempByteArr.position = 0;
- return tempByteArr.readMultiByte(tempByteArr.bytesAvailable, "chinese");
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...