注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

yangzh691216的博客

成功止于信念

 
 
 

日志

 
 

纯代码频谱播放器  

2017-07-30 12:25:29|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


在第一帧插入:

/***
*
*发布大小随意
*author@火鸟
*
***/
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.filters.DropShadowFilter;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.events.ContextMenuEvent;
import flash.net.navigateToURL;

 

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
this.graphics.beginFill(0);
this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);

 

/*声音地址*/
var url:String = "
http://wp.zyxqw.com/wp/userfiles/yangzh691216/03/ldgb.swf";
var sound:Sound;
var soundChannel:SoundChannel=new SoundChannel();
var _transform:SoundTransform=new SoundTransform();

 

var mcX:int = stage.stageWidth - 10;
var mcY:int = stage.stageHeight;

 

var playButton:Sprite;
var pauseButton:Sprite;
var seekControlBar:Sprite;
var playheadBar:Sprite;
var loadBar:Sprite;
var playTime:TextField;

 

/*播放中的布尔值*/
var isPlay:Boolean = false;

 

/*鼠标按下的布尔值*/
var isMouseDown:Boolean;

 

/*记录总长度*/
var duration:Number;

 

/*记录播放头的位置*/
var seekpoint:Number = 0;

 

/*初始化按钮、播放进度条*/
initControl();

 

/*开始加载声音播放*/
initPlay();

 


function initPlay():void
{
 sound=new Sound();
 sound.load(new URLRequest(url));
 sound.addEventListener(ProgressEvent.PROGRESS,progressHandler);
 sound.addEventListener(Event.COMPLETE,completeHandler);
 sound.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
 soundChannel = sound.play();
 soundChannel.soundTransform = _transform;
 soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
 addEventListener(Event.ENTER_FRAME,enterFrameHandler);
 isPlay = false;
}

 

function progressHandler(event:ProgressEvent):void
{
 //trace("开始加载");
 var loadPercentage:Number = sound.bytesLoaded / sound.bytesTotal;
 if (loadPercentage>0&&loadPercentage!=Infinity)
 {
  loadBar.width = loadPercentage * seekControlBar.width;
 }
 else
 {
  loadBar.width = 1;
 }
}

 

function completeHandler(event:Event):void
{
 trace("加载完成");
}

 

function ioErrorHandler(event:IOErrorEvent):void
{
 trace("加载错误");
}

 

function soundCompleteHandler(event:Event):void
{
 trace("播放完成");
 initPlay();
}

 

function enterFrameHandler(event:Event):void
{
 duration=sound.length/(sound.bytesLoaded/sound.bytesTotal);
 soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);

 

 if (isMouseDown)
 {
  if (soundChannel.position > 0 && duration > 0)
  {
   playheadBar.width = mouseX - seekControlBar.x;
   playTime.htmlText = "<font face=\"Verdana\" size=\"10\" color=\"#ffffff\">" + millToTime((mouseX - seekControlBar.x) / seekControlBar.width * duration) + "|" + millToTime(duration) + "</font>";
  }
 }
 else
 {
  if (soundChannel.position > 0 && duration > 0)
  {
   playheadBar.width = soundChannel.position / duration * seekControlBar.width;
  }
  else
  {
   playheadBar.width = 1;
  }
  playTime.htmlText = "<font face=\"Verdana\" size=\"10\" color=\"#ffffff\">" + millToTime(soundChannel.position) + "|" + millToTime(duration) + "</font>";

 

 }

}

 

function initControl():void
{
 /*播放按钮*/
 playButton=new Sprite();
 playButton.graphics.beginFill(0xffffff,1);
 playButton.graphics.drawRect(2, 2, 1, 12);
 playButton.graphics.moveTo(3,2);
 playButton.graphics.lineTo(3,14);
 playButton.graphics.lineTo(11,8);
 playButton.graphics.lineTo(3,2);
 playButton.graphics.beginFill(0xff0000,0);
 playButton.graphics.drawRect(0,0, 14, 14);
 playButton.graphics.endFill();
 addChild(playButton);
 playButton.filters = [new DropShadowFilter(1,30,0x000000,2,2,1,1)];
 playButton.buttonMode = true;
 playButton.y = mcY - 30;
 playButton.x = 30;

 

 /*暂停按钮*/
 pauseButton=new Sprite();
 pauseButton.graphics.beginFill(0xffffff,1);
 pauseButton.graphics.drawRect(2, 3, 3,10);
 pauseButton.graphics.drawRect(7, 3, 3, 10);
 pauseButton.graphics.beginFill(0xff0000,0);
 pauseButton.graphics.drawRect(0, 0, 14, 14);
 pauseButton.graphics.endFill();
 addChild(pauseButton);
 pauseButton.filters = [new DropShadowFilter(1,30,0x000000,2,2,1,1)];
 pauseButton.buttonMode = true;
 pauseButton.x = 8;
 pauseButton.y = mcY - 30;

 


 /*播放进度条*/
 seekControlBar=new Sprite();
 seekControlBar.graphics.beginFill(0xffffff,0.3);
 seekControlBar.graphics.drawRect(0,0,mcX-135,1);
 seekControlBar.graphics.endFill();
 addChild(seekControlBar);
 seekControlBar.x = 135;
 seekControlBar.y = mcY - 20;

 

 loadBar=new Sprite();
 loadBar.graphics.beginFill(0xffffff,0.4);
 loadBar.graphics.drawRect(0,0,1,1);
 loadBar.graphics.endFill();
 addChild(loadBar);
 loadBar.x = 135;
 loadBar.y = mcY - 20;

 

 playheadBar=new Sprite();
 playheadBar.graphics.beginFill(0x660000,1);
 playheadBar.graphics.drawRect(0,0,1,1);
 playheadBar.graphics.endFill();
 addChild(playheadBar);
 playheadBar.x = 135;
 playheadBar.y = mcY - 20;
 playheadBar.mouseEnabled = false;
 
 /*播放时间文本*/
 playTime=new TextField();
 playTime.autoSize = TextFieldAutoSize.LEFT;
 playTime.selectable = false;
 addChild(playTime);
 playTime.x = 52;
 playTime.y = mcY - 30;
}

 


/*按钮侦听事件*/
playButton.addEventListener(MouseEvent.CLICK,playButtonClickHandler);
pauseButton.addEventListener(MouseEvent.CLICK,pauseButtonClickHandler);

 

/*播放进度控制事件*/
loadBar.addEventListener(MouseEvent.MOUSE_DOWN,loadBarDownHandler);

 

function playButtonClickHandler(event:MouseEvent):void
{
 if (isPlay)
 {
  soundChannel.stop();
  soundChannel = sound.play(seekpoint);
 }
}

 


function pauseButtonClickHandler(event:MouseEvent):void
{
 isPlay = true;
 seekpoint = soundChannel.position;
 soundChannel.stop();
}

 


function loadBarDownHandler(event:MouseEvent):void
{
 isMouseDown = true;
 isPlay = false;
 stage.addEventListener(MouseEvent.MOUSE_UP,loadBarUpHandler);
 playheadBar.width = mouseX - seekControlBar.x;
}

 


function loadBarUpHandler(event:MouseEvent=null):void
{
 isMouseDown = false;

 

 stage.removeEventListener(MouseEvent.MOUSE_UP,loadBarUpHandler);
 seekpoint = (mouseX - seekControlBar.x) / seekControlBar.width * duration;
 soundChannel.stop();
 soundChannel = sound.play(seekpoint);
}

 


function millToTime(mill:Number):String
{
 mill = mill / 1000;
 var minutes = int(mill / 60);
 var seconds = int(mill % 60);
 minutes < 10 ? minutes = "0" + minutes:null;
 seconds < 10 ? seconds = "0" + seconds:null;
 return minutes+ ":" + seconds;
}

 

/**************鼠标右键***********/
var myContextMenu:ContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();
this.contextMenu = myContextMenu;
var menuItem:ContextMenuItem = new ContextMenuItem("Author:火鸟",true);
myContextMenu.customItems.push(menuItem);
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,link);
function link(event:ContextMenuEvent):void
{
 var url:URLRequest = new URLRequest("
http://yangzh691216.blog.163.com/");
 navigateToURL(url,"_blank");
}

 

 

/*****************频谱***************/
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.display.Shape;
import flash.display.GradientType;
import flash.net.URLRequest;
import flash.system.Security;
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.ByteArray;
import flash.media.SoundMixer;
import flash.events.Event;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Point;
import flash.display.StageAlign;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.display.StageScaleMode;
import flash.geom.Rectangle;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;

 

var _sprite:Sprite;
var timer:Timer;
var thisW:Number;
var thisH:Number;
var pos:Number = 0;

 

var byteArray:ByteArray;

var sDis:Number = 30;
var lDis:Number = 100;
var maxDis:Number;
var bitmapData:BitmapData;
var bitmap:Bitmap;
var color:ColorTransform = new ColorTransform(0.90,1,1,0.90);
var numArr:Array = [];

 

spectrum(stage.stageWidth,stage.stageHeight);

function spectrum(width:Number=550,height:Number=400)
{
 thisW = width;
 thisH = height;
 maxDis = height / 4;
 byteArray= new ByteArray();
 _sprite = new Sprite();
 for (var i:int= 0; i < 256; ++ i)
 {
  numArr[i] = 0;
 }

 

 bitmapData = new BitmapData(thisW,thisH,true,0);
 bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;
 addChild( _sprite );

 

 timer = new Timer(30);
 timer.addEventListener(TimerEvent.TIMER,timerHandler);
 timer.start();

 

}

function timerHandler(event:TimerEvent):void
{
 var aPower:Number = 3;
 var num:Number;

 

 SoundMixer.computeSpectrum( byteArray, false );

 aPower /=  2;
 _sprite.graphics.clear();
 _sprite.graphics.lineStyle( 1, 0xFF00FF );
 for (var i:int= 0; i < 128; i++)
 {
  num = Math.abs(byteArray.readFloat() + 0.1);
  numArr[ i ] += (num-numArr[ i ] ) * 0.25;
  num = numArr[i];
  _sprite.graphics.drawCircle( thisW/2 + Math.sin(i * 360 / 128 * Math.PI / 180) * (sDis+num*maxDis),thisH/2 + Math.cos(i * 360 / 128 * Math.PI / 180) * (sDis+num*maxDis), num *30 );

 


 }

 

 _sprite.graphics.beginFill( 0xFF33FF, 1 );
 for (i = 0; i <128; ++ i)
 {
  num = Math.abs(byteArray.readFloat() + 0.1);
  numArr[ i ] += (num-numArr[ i ] ) * 0.35;
  _sprite.graphics.drawCircle( thisW/2+ Math.sin(i * 360 /128 * Math.PI / 180) * (lDis+num*maxDis),thisH/2 + Math.cos(i * 360 /128 * Math.PI / 180) * (lDis+num*maxDis),num * 10 );
 }
 bitmapData.draw( _sprite );
 bitmapData.scroll( aPower *Math.sin(pos)*4, aPower *Math.cos(pos)*2);
 bitmapData.colorTransform( bitmapData.rect, color );
 bitmapData.applyFilter( bitmapData, bitmapData.rect, new Point, new BlurFilter(2,2));
 bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new DisplacementMapFilter(bitmapData,new Point(),1,2,5,5,"clamp"));
 pos +=  0.02;
}

  评论这张
 
阅读(16)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017