Sound类的length和SoundChannel类的position之间的关系

本文通过实例演示了在Flash中使用SoundChannel.position与Sound.length来判断音频播放状态的方法存在问题,即position始终小于length,不适合用来判断音乐是否播放完毕。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个例子用于验证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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值