天天看点

ActionScript3 使用sound类(e文翻译)——播放进度条

 我们想知道当前的歌曲已经播放了百分之几(意译)

解决方法

    用Sound.length获取声音文件的长度,用SoundChannel.position获取当前播放了多少。

具体讨论成业原创,转载说明出处 http://hi.baidu.com/flash_as3

       问题六[原创]ActionScript3 使用sound类(e文翻译)——获取文件大小中,我们提到了如何添加一个不仅显示下载进度还显示播放进度的进度条,那一节中学习了创建显示下载进度的部分。

       成业原创,转载说明出处 http://hi.baidu.com/flash_as3

       本节讨论另外一个部分:如何跟踪播放进度。为了实现这个目标,我们必须知道两个量:当前文件播放位置是多少和文件总长度。虽然它们看起来差不多,但是它们却在不同的类中,文件长度信息是sound对象的一个属性,当前播放位置在SoundChannel类中。类似于问题六[原创]ActionScript3 使用sound类(e文翻译)——获取文件大小中创建已下载进度条一样,有这两个量很容易得到播放进度条。

       成业原创,转载说明出处 http://hi.baidu.com/flash_as3

    不幸的是,播放进度条做起来比下载进度条要复杂,因为声音文件的总长度要等到声音文件全部下载完毕之后才能得到,此时读取文件长度只是得到了已经下载的声音文件的长度。因此,比如:以个十分钟的音乐,下载了10%,它的长度(length)是一分钟。(事实上,length和position都返回毫秒为单位的数据,如果需要,我们也可以把它转换成“分:秒”的表示形式)

       成业原创,转载说明出处 http://hi.baidu.com/flash_as3

       幸运的是,我们只要经过一点小小的数学运算就能得到文件的总长度(毫秒),我们拿当前文件的长度(毫秒)除以当前已经下载下来的百分比,结果将非常接近文件的实际长度(毫秒)。说一下刚刚提到的例子:长度返回值是1分钟,这个一分钟是指已经下载的文件长度。当我们拿这个一分钟除以1/10,就能得到结果是10,这样我们就得到了文件的总长度是10分钟。

    成业原创,转载说明出处 http://hi.baidu.com/flash_as3

上面提到的百分比是用bytesLoaded/bytesTotal得到的,这个值是我们在做下载进度条时候用过并存起来的,所以所以文件总长度只要一行代码就能实现了:

    成业原创,转载说明出处 http://hi.baidu.com/flash_as3

length /= percentBuffered;

       成业原创,转载说明出处 http://hi.baidu.com/flash_as3

说明:问题六[原创]ActionScript3 使用sound类(e文翻译)——获取文件大小中,我们提到了要想正确显示进度条要清空缓存的问题,在此不再重复。

       成业原创,转载说明出处 http://hi.baidu.com/flash_as3

下面的代码展示了如何把两个进度条放在一起:

package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.events.Event;
       
    public class ProgressBar2 extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        
        public function ProgressBar2(  ) {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            _sound = new Sound(new URLRequest("song.mp3"));
            _channel = _sound.play(  );
        }
              
        public function onEnterFrame(event:Event):void{

           var barWidth:int = 200;
            var barHeight:int = 5;            
            var loaded:int = _sound.bytesLoaded;
            var total:int = _sound.bytesTotal;
            var length:int = _sound.length;
            var position:int = _channel.position;                 
            // Draw a background bar      
            graphics.clear(  );
            graphics.beginFill(0xFFFFFF);
            graphics.drawRect(10, 10, barWidth, barHeight);
            graphics.endFill(  );       
            if(total > 0) {      
                // The percent of the sound that has loaded      
                var percentBuffered:Number = loaded / total;       
                // Draw a bar that represents the percent of       
                // the sound that has loaded      
                graphics.beginFill(0xCCCCCC);
                graphics.drawRect(10, 10, 
                                  barWidth * percentBuffered,
                                  barHeight);      
                graphics.endFill(  );      
                 // Correct the sound length calculation      
                length /= percentBuffered;                      
                  // The percent of the sound that has played      
                  var percentPlayed:Number = position / length;       
                // Draw a bar that represents the percent of       
                // the sound that has played      
                graphics.beginFill(0x666666);
                graphics.drawRect(10, 10, 
                                  barWidth * percentPlayed,
                                  barHeight);
                graphics.endFill(  );      
            }      
        }      
    }          
}      

译者注:上面的这个例子的使用方法:

1,新建立一个fla文件,命名,保存;

2,找首歌曲(mp3格式)复制到fla的那个目录里面,命名为song.mp3(mp3是后缀名);

3,新建一个.as文件,把上述代码复制到其中,保存在fla同一个目录,命名为ProgressBar2.as;

4,把fla文件的document class属性(选中舞台,打开属性面板就能看到)设置为ProgressBar2,设置背景颜色为兰色(只要不是白色黑色灰色就行);

5,按ctrl+enter测试。我们看到效果。

如果看不到加载进度,请换个网络上的歌曲试试看。