as3 声音控制

SoundUtil.as源代码:

/*****************************************************
 *  
 *  The Initial Developer of the Original Code is Jave.Lin(afeng).
 *  
 *  SoundUtil.as
 *  Create By Jave.Lin(afeng)
 *  2012-10-47 上午17:15:13
 *  
 *****************************************************/
package
{
	import com.greensock.TweenMax;
	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	/**
	 * 声音工具类
	 * @author Jave.Lin(afeng)
	 * 
	 */	
	public class SoundUtil{
		
		private var _loopCount:int;
		private var _sound:Sound;
		private var _channel:SoundChannel;
		private var _position:int;
		
		public static function getInstance():SoundUtil{
			return new SoundUtil()
		}
		
		public function SoundUtil(loopCount:int=int.MAX_VALUE):void{
			_loopCount=loopCount;
			_sound=new Sound();
		}
		
		public function loadUrl(url:String,onComplete:Function=null):void{
			var urlLoader:URLLoader=new URLLoader();
			urlLoader.dataFormat=URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE,onLoaderComplete);
			function onLoaderComplete(event:Event):void{
				var bytes:ByteArray=event.target.data as ByteArray;
				loadBytes(bytes,bytes.length);
				urlLoader.removeEventListener(Event.COMPLETE,onLoaderComplete);
				if(onComplete!=null)onComplete();
			}
			urlLoader.load(new URLRequest(url));
		}
		
		public function loadBytes(bytes:ByteArray,len:uint):void{
			_sound.loadCompressedDataFromByteArray(bytes,len);
		}
		
		public function stop():void{
			if(!_channel)return;
			TweenMax.killTweensOf(_channel,true);
			_position=0;
			easeVolume(0,function():void{
//				trace("stop complete");
				_channel.stop();
			});
		}
		
		public function pause():void{
			if(!_channel)return;
			TweenMax.killTweensOf(_channel,true);
			_position=_channel.position;
			easeVolume(0,function():void{
//				trace("pause complete");
				_channel.stop();
			});
		}
		
		public function play():void{
			if(_channel)TweenMax.killTweensOf(_channel,true);
			var stf:SoundTransform=new SoundTransform(0);
			_channel=_sound.play(_position,int.MAX_VALUE,stf);
			easeVolume(1);
		}
		
		private function easeVolume(value:Number,onComplete:Function=null):void{
			TweenMax.killTweensOf(_channel,true);
			TweenMax.to(_channel,1,
				{
					volume:value,
					onComplete:onComplete
				});
		}
	}
}



调用类:

package
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	[SWF(width="1200",height="720")]
	public class testmusic extends Sprite
	{
		private var _playBtn:Button;
		private var _pauseBtn:Button;
		private var _stopBtn:Button;
		
		private var _sound:SoundUtil;
		
		private var _position:int;
		
		public function testmusic()
		{
			super();
			
			stage.color=0xeeeeee;
			stage.frameRate=60;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			
			var url:String="res/m008.mp3";//你电脑上的MP3音乐文件名,把该文件与MP3音乐文件放在一个文件夹内。
			
			
			_sound=SoundUtil.getInstance();
			_sound.loadUrl(url,onSoundComplete);
			
//			var _request:URLRequest = new URLRequest(url);
//			_sound.load(_request);
//			_sound.addEventListener(Event.COMPLETE,onSoundComplete);
			
			_playBtn=new Button("play");
			_pauseBtn=new Button("pause");
			_stopBtn=new Button("stop");
			
			addChild(_playBtn);
			addChild(_pauseBtn);
			addChild(_stopBtn);
			
			_playBtn.x=100;
			_playBtn.y=100;
			
			_pauseBtn.x=100;
			_pauseBtn.y=150;
			
			_stopBtn.x=100;
			_stopBtn.y=200;
			
			_playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClick);
			_pauseBtn.addEventListener(MouseEvent.CLICK,onPauseBtnClick);
			_stopBtn.addEventListener(MouseEvent.CLICK,onStopBtnClick);
			
//			var transform:SoundTransform = channel.soundTransform;
//			transform.volume = volume;
//			channel.soundTransform = transform;
		}
		
		private function onSoundComplete():void{
			trace("load sound complete");
		}
		
		private function onStopBtnClick(event:MouseEvent):void{
			_sound.stop();
		}
		
		private function onPauseBtnClick(event:MouseEvent):void{
			_sound.pause();
		}
		
		private function onPlayBtnClick(event:MouseEvent):void{
			_sound.play();
		}

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值