AS3之麦克风接口【flash.media.Microphone 类】
package { import flash.display.Shape; import flash.display.Sprite; import flash.events.ActivityEvent; import flash.events.Event; import flash.media.Microphone; public class SoundGame extends Sprite { private var main:Sprite; private var mic:Microphone; private var minHigh:int = 0; private var intervalValue:int = 0; public function SoundGame() { init(); } //初始化 private function init():void{ initMic(); createSky(); createLand(); createMain(); } //初始化麦克风 private function initMic():void{ mic = Microphone.getMicrophone(); mic.setSilenceLevel(30,0); mic.setLoopBack(); mic.addEventListener(ActivityEvent.ACTIVITY,micActivityHandle); } //创建操作 private function createMain():void{ var mainContainer:Sprite = new Sprite; main = new Sprite; main.graphics.beginFill(0xffffff); main.graphics.drawCircle(-2,-4,4); mainContainer.x = 100; mainContainer.y = 250; mainContainer.addChild(main); addChild(mainContainer); main.addEventListener(Event.ENTER_FRAME,frameHandle); } //创建天 private function createSky():void{ var shape:Shape = new Shape; shape.graphics.beginFill(0x6699cc,1); shape.graphics.drawRect(0,0,400,250); addChild(shape); } //创建陆地 private function createLand():void{ var shape:Shape = new Shape; shape.graphics.beginFill(0x002222,1); shape.graphics.drawRect(0,250,400,150); addChild(shape); } //麦克风活跃处理 private function micActivityHandle(e:ActivityEvent):void{ if(e.activating && intervalValue == 0){ intervalValue = mic.activityLevel / 5; } } //持续处理 private function frameHandle(e:Event):void{ if(intervalValue == 0) return; main.y -= intervalValue; intervalValue --; if(intervalValue == 0) intervalValue -= 1; //落地 var min:int = - minHigh; if(min < main.y){ main.y = min; intervalValue = 0; } } } }