您当前的位置: 首页 > 

Jave.Lin

暂无认证

  • 3浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

as3 声音控制

Jave.Lin 发布时间:2012-10-24 17:53:11 ,浏览量:3

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();
		}

	}
}

关注
打赏
1664331872
查看更多评论
立即登录/注册

微信扫码登录

0.0385s