Flex4实现 音频播放器 显示语音波形

直接上代码,在Flash Builder 4中新建 FLEX项目,复制代码可以可以直接运行

需要将 file = "D:\\voice\\3791.MP3"; 改为自己的路径

以后还准备增加流式播放!

 



Flex4实现 音频播放器 显示语音波形_第1张图片

 



	
		
	
	
		
	
	
	
		0){ 
					bufferRect.width = sound.bytesLoaded / sound.bytesTotal*(proccessBar.width);
				}  
			}
			
			private function onProccessBarDown(e:MouseEvent):void{
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
			
			private function onProccessBarUp(e:MouseEvent):void{
				this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
			
			//读取语音数据,一边取一边画
			private function loaded(event:Event):void {
				var width:Number = box.width;
				var height:Number =box.height;
				
				//获取语音长度,毫秒
				var lengthTime:Number = sound.length;
				totalTime = formatTime(lengthTime);
				playTime.text = totalTime;
				
				_sperite = new Sprite();//建立影片精灵   
				var uicomponent:UIComponent = new UIComponent();  
				uicomponent.addChild(_sperite);  
				box.addElement(uicomponent);
				
				doDraw(0.1,0xffffff,0,height/2,width-2,height/2);  //绘制中心轴线
				//绘制横轴
				for(var i:int=20;i 0){
					var value:Number = ba.readFloat();
					var endX:Number = m*width/dataCount;
					var endY:Number = height/2;
					endY = (1-value)*(height/2);//正数
					doDraw(0.1, 0x00FF00, startX, startY, endX, endY);//绘制波形
					startX = endX; startY = endY;
					if(ba.bytesAvailable > 0){
						ba.position += 84;
						m += 11;
					}
				}
				
				//完成自动播放
				playState=1;
				chanel = sound.play(stopPosition);
				chanel.soundTransform=vol;			
				this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
				playBtn.visible=false;
				pauseBtn.visible=true;
				pState.text = "状态: 播放";
				
				load.visible = false;
			}
			
			//绘制曲线
			private function doDraw(cx:Number,color:uint,startX:Number,startY:Number,endX:Number,endY:Number):void{  
				_sperite.graphics.lineStyle(cx,color,1);  
				_sperite.graphics.moveTo(startX,startY);  
				_sperite.graphics.lineTo(endX,endY);  
			}  
			
			//播放暂停
			private function onPlayBtnClick(e:MouseEvent):void{
				//正常状态控制播放与暂停
				if(playState==1){
					playState=0;	
					stopPosition = chanel.position; 
					chanel.stop();
					this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
					playBtn.visible=true;
					pauseBtn.visible=false;
					pState.text = "状态: 暂停";
				}else if(playState==0){
					playState=1;
					chanel = sound.play(stopPosition);
					chanel.soundTransform=vol;			
					this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
					playBtn.visible=false;
					pauseBtn.visible=true;
					pState.text = "状态: 播放";
				}
			}
			
			//停止播放
			public function musStop():void{
				playState=0;
				stopPosition = 0; 
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				proccessBar.value=0;
				playTime.text="00:00/"+totalTime;
				playBtn.visible=true;
				pauseBtn.visible=false;
				pState.text = "状态: 停止";
				if(chanel!=null){
					chanel.stop();
				}
			}
			
			//静音处理按钮
			private function onSoundClick(e:MouseEvent):void{
				//点击正常状态的静音按钮处理函数
				if(soundState==1){
					soundState=0;
					vol.volume=0;
					soundValue = soundSlide.value;
					soundSlide.value=0;
					soundBtnJ.visible=true;
					soundBtn.visible=false;
				}else if(soundState==0){
					soundState=1;
					soundSlide.value = soundValue;
					vol.volume=soundSlide.value;
					soundBtnJ.visible=false;
					soundBtn.visible=true;
				}
				chanel.soundTransform=vol;
			}
			
			//控制音量大小
			private function soundSlideChange():void{
				vol.volume=soundSlide.value;
				chanel.soundTransform=vol;
			}
			
			//控制播放进度
			private function onEnterFrame(e:Event):void{
				if(chanel!=null){
					proccessBar.value=chanel.position*(100/sound.length);
					playTime.text=formatTime(chanel.position) + "/" + totalTime;
					if(proccessBar.value>99){
						musStop();
					}
				}
			}
			
			//鼠标拖动进度条更改播放进度的处理
			private function changePos():void{
				if(chanel!=null){
					chanel.stop();
					//完成自动播放
					playState=1;
					chanel = sound.play(proccessBar.value*sound.length/100);
					chanel.soundTransform=vol;			
					this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
					playBtn.visible=false;
					pauseBtn.visible=true;
					pState.text = "状态: 播放";
				}
			}
			
			//时间格式处理
			private function formatTime(time:Number):String{
				var s:String;
				var m:Number = Math.floor(time / 60000);//分钟
				if(m >= 60){
					//小时
					var h:Number = Math.floor(m / 60);
					if (h < 10)
						s = "0" + h;
					else
						s = String(h);
					
					//分钟
					m = m - h*60;
					if (m < 10)
						s += ":0" + m;
					else
						s += ":" + String(m);
				}else{
					if (m < 10)
						s = "0" + m;
					else
						s = String(m);
				}
				
				m = Math.floor((time / 1000 ) % 60)
				if (m < 10)
					s += ":0" + m;
				else
					s += ":" + m;
				
				return s;
			}
			
			//从Url中得到参数,得到   ? 以后的所有
			private function getParams():Object {
				params = {};
				//获取整个URI
				var query:String = ExternalInterface.call("window.location.search.substring", 1);
				var url:String = ExternalInterface.call("window.location.href.toString", 1);
				var index:int = url.indexOf("dsas")
				serverIP = url.substring(0,index-1);
				
				if(query) {
					var pairs:Array = query.split("&");
					for(var i:uint=0; i < pairs.length; i++) {
						var pos:int = pairs[i].indexOf("=");
						if(pos != -1) {
							var argname:String = pairs[i].substring(0, pos);
							var value:String = pairs[i].substring(pos+1);
							params[argname] = value;
						}
					}
				}
				return params;
			}
			
			//下载语音文件
			private function downLoadsVoice():void{
				var args:Object = getParams();      //获取URL中的参数列表
				var fileName:String = args.audio;
				var file:String = serverIP + fileName + ".mp3";//得到语音文件名
				var urlReq:URLRequest = new URLRequest(file);  
				var fileRef:FileReference = new FileReference(); 
				var index:int = fileName.indexOf("voice/");
				var tempFile:String = fileName.substring(index+6,fileName.length)+".mp3";
				fileRef.download(urlReq,tempFile);
			}
		]]>
	
	
	
		
		
		
		
			
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

 

 

 

 

 

 

你可能感兴趣的:(Flex)