写了一个例子用于验证length和position间的不匹配。
/** * 本例验证不能通过SoundChannel.position和Sound.length进行比较以判断音乐是否结束 * */ package { import flash.display.Graphics; import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.URLRequest; import flash.utils.Timer; public class _Sound extends Sprite { var _req:URLRequest; var _sound:Sound; var _channel:SoundChannel; var _rect:Sprite; var t:Timer; public function _Sound() { _req = new URLRequest("daoxiang.mp3"); _sound = new Sound(_req); _rect = new Sprite(); var g:Graphics = _rect.graphics; g.beginFill(0xcc0000); g.drawRect(0,0,100,100); this.addChild(_rect); _rect.x = (this.stage.stageWidth-_rect.width)/2; _rect.y =(this.stage.stageHeight-_rect.height)/2; _rect.addEventListener(MouseEvent.CLICK,onClick); t = new Timer(0); } private function onClick(e:MouseEvent):void{ if(_channel==null){ //从声音长度96%处开始播放,以减少测试时间 _channel = _sound.play(_sound.length*.96); t.start(); t.addEventListener(TimerEvent.TIMER,onProgress); } } private function onProgress(e:TimerEvent):void{ if(_channel.position<_sound.length){ trace(_channel.position+" / "+_sound.length); _rect.rotation+=5; } } } }
测试最终数据为:
223491.13378684808 / 223660.4081632653
由此可见,position总是小于length,无论任何时候!因此在播放过程中不能使用SoundChannel类对象.position<Sound类对象.length。