AS3(flash)獲取MP3音訊檔案的位元率
阿新 • • 發佈:2019-01-29
基本的演算法是:【位元速率】(kbps)=【檔案大小】(位元組)X8/【時間】(秒)/1024
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.media.Sound; import flash.net.FileFilter; import flash.net.FileReference; public class GetMp3BitRateDemo extends Sprite { private var _fileRefer:FileReference; public function GetMp3BitRateDemo() { stage.addEventListener(MouseEvent.CLICK,onClick); } private function onClick(e:MouseEvent):void { _fileRefer = new FileReference(); _fileRefer.addEventListener(Event.SELECT,onSelect); _fileRefer.addEventListener(Event.COMPLETE,onComplete); _fileRefer.browse([new FileFilter("Images (*.mp3)", "*.mp3")]); } private function onSelect(e:Event):void { _fileRefer.load();//本地載入 } private function onComplete(e:Event):void { if(!_fileRefer || !_fileRefer.data) return; var mp3:Sound = new Sound(); mp3.loadCompressedDataFromByteArray(_fileRefer.data,_fileRefer.data.length); var bitRate:Number = Math.round((_fileRefer.size * 8) / (mp3.length/1000) / 1024); trace("bitrate == " + bitRate); _fileRefer.cancel(); _fileRefer = null; } } }
Enjoy it.