AS代码制作声控图片【附代码】
1、新建一个AS3.0文档,拖入一张图片到库,点插入,新建一个影片剪辑元件(取名:图片元件),再拖入图片, 2、点插入新建影片剪辑元件,拖入“图片元件”,然后随意做个几桢的动画。最后一桢把图片放大,或设置透明度等。 3、拖入图片动画到场景取实例名为“mc”,再新建一层AS代码层,复制以下代码贴上及可。import flash.media.SoundMixer;import flash.events.Event;import flash.media.Sound;import flash.net.URLRequest;import flash.events.MouseEvent;import flash.ui.ContextMenu;import flash.ui.ContextMenuBuiltInItems;stage.scaleMode = StageScaleMode.NO_SCALE;stage.align = StageAlign.TOP_LEFT;var menu:ContextMenu=new ContextMenu();menu.hideBuiltInItems();this.contextMenu = menu;var sound:Sound=new Sound();var sc:SoundChannel=new SoundChannel();var position:Number;var isPlay:Boolean;var byteArray:ByteArray= new ByteArray();sound.load(new URLRequest("http://img0.oldkids.cn/upload/2021/05/20/blog_260848287_20210520142951772.mp3 "));sc = sound.play();this.addEventListener(MouseEvent.CLICK,onClickHandler);sc.addEventListener(Event.SOUND_COMPLETE,scCompleteHandler);function onClickHandler(event:MouseEvent):void{isPlay = ! isPlay;if (isPlay){position = sc.position;sc.stop();}else{sc.stop();sc = sound.play(position);}sc.addEventListener(Event.SOUND_COMPLETE,scCompleteHandler);}function scCompleteHandler(event:Event):void{sc = sound.play();sc.addEventListener(Event.SOUND_COMPLETE,scCompleteHandler);}this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);function enterFrameHandler(event:Event):void{SoundMixer.computeSpectrum(byteArray,true);var ratio:Number = range(Math.round(byteArray.readFloat() / 1.414 * mc.totalFrames));//var ratio:Number = range(Math.round((sc.leftPeak+sc.rightPeak)*.5 * mc.totalFrames));mc.gotoAndStop(ratio);}function range(value:Number):Number{if (value>1){return value;}else{return 1;}}