Sound recording with timer event - actionscript-3

I got this code for recording users sound:
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(waveEncoder);
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
public function Main():void
{
recButton.stop();
activity.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
Security.showSettings("2");
addListeners();
}
private function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
recorder.addEventListener(RecordingEvent.RECORDING, recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}
private function startRecording(e:MouseEvent):void
{
if (mic != null)
{
recorder.record();
e.target.gotoAndStop(2);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, -recBar.height,0,1,true);
}
}
private function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}
private function updateMeter(e:Event):void
{
activity.gotoAndPlay(100 - mic.activityLevel);
}
private function recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function recordComplete(e:Event):void
{
fileReference.save(recorder.output, "recording.wav");
}
}
I want to replace mouse events with timer event. If lasted time == 5 then start recording and after
10 seconds stop recording. I confused where add my timer code something like this:
var myIntrotime:Timer = new Timer(1000,5);
myIntrotime.addEventListener(TimerEvent.TIMER, startIntroTime);
myIntrotime.start();
var SecondsElapsed:Number = 1;
function startIntroTime(event:TimerEvent):void
{
if (SecondsElapsed==5)
{
//start recording
//start another timer2 and if timer2 finished stop recording
}
SecondsElapsed++;
}
Could someone help me?

You can do better if you employ flash.utils.setTimeout() to make a delayed call. This makes all the dirty work with timers for you.
setTimeout(startIntroTime,5000)
function startIntroTime():void
{
//start recording
setTimeout(stopRecording,10000);
}
The manual on setTimeout()

Related

use of button to increase and decrease the timer in action script3.0

I have written a code to add two buttons. It shows them correctly, but the action is not performed correctly.
public class butt extends Sprite {
public function butt() {
var delayGlobal:Number = 2000;
var min1:Number =1000;
var myTimer:Timer = new Timer(delayGlobal);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
function myButton1Click(ev:MouseEvent):void {
delayGlobal = delayGlobal- 1000;
trace(delayGlobal);
}
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
function myButton2Click(ev:MouseEvent):void {
delayGlobal = delayGlobal + 1000;
trace(delayGlobal);
}
function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}
}
The timer is shown but doesn't work
Your issue is that you are updating a variable, but not actually updating the timer at all.
You need to explicitly tell the timer to change the delay. myTimer.delay = delayGlobal
Here is a re-working of your code:
public class butt extends Sprite {
private var min1:Number =1000; //assuming this is your minimum allowed delay?
private var myTimer:Timer;
public function butt() {
myTimer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
}
private function myButton1Click(ev:MouseEvent):void {
myTimer.delay = Math.max(min1, myTimer.delay - 1000); //This will assign whatever is bigger, min1 or the timer delay less 1000 - ensuring that the timer doesn't drop below the value of min1
trace(myTimer.delay);
}
private function myButton2Click(ev:MouseEvent):void {
myTimer.delay = myTimer.delay + 1000;
trace(myTimer.delay);
}
private function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}

How do I stop my Flash (AS3) program's framerate from dropping when users go to a new tab in their browser?

Every time I run my Flash program in a browser, the framerate drops from 30 to ~2 whenever I go to a different tab. Is there a way to stop the background framerate from tanking whenever the window isn't in focus? The framerate doesn't go down if I open a new window in the browser, even if the window covers the window with the Flash program. I'm also working in FlashDevelop if that helps. Thanks!
Unearthed some source code from 2014 and found what I ended up going with:
HUD.as:
public var idle:Boolean = false;
public var catchingUp:Boolean = false;
private var secondsIdle:int = 0;
stage.addEventListener(Event.DEACTIVATE, StartIdleMode);
stage.addEventListener(Event.ACTIVATE, RegainedFocus);
public function Update(sta:DisplayObjectContainer, save:SharedObject):void
{
//...The game logic that happens every frame...
if (secondsIdle < 0)
{
idle = true;
}
}
private function StartIdleMode(e:Event):void
{
var date:Date = new Date();
secondsIdle = date.valueOf();
}
private function RegainedFocus(e:Event):void
{
if (idle)
{
var diffDate:Date = new Date();
secondsIdle += -(diffDate.valueOf() / 2);
catchingUp = true;
}
}
public function CatchUp(sta:DisplayObjectContainer, save:SharedObject):void
{
var tempFrames:int = Math.floor(secondsIdle / 1000 * 30);
while (tempFrames < 0)
{
this.Update(sta, save);
tempFrames++;
}
idle = false;
catchingUp = false;
secondsIdle = 0;
}
Main.as:
private var gameTimer:Timer = new Timer(1000 / 30, 0);
private var hud:HUD;
public function Main():void
{
gameTimer.addEventListener(TimerEvent.TIMER, GameLoop);
gameTimer.start();
}
private function GameLoop(e:TimerEvent):void
{
if (!hud.idle)
{
hud.Update(stage, gameSystem.saveData);
}
if (hud.catchingUp)
{
hud.CatchUp(stage, gameSystem.saveData);
}
}

Away3d mesh not getting mouseClick events

I am just trying to get the coordinates of the mouse in a plane but the mouse event just doesnt fire.
I also have an starling instance on top of away3d that i got with this tutorial and i believe there is a problem there.
Here is my code:
public class Main extends Sprite
{
private var view3D:View3D;
private var stage3DManager:Stage3DManager;
private var stage3DProxy:Stage3DProxy;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
view3D = new View3D();
initProxies();
}
private function initProxies():void
{
stage3DManager = Stage3DManager.getInstance(stage);
stage3DProxy = stage3DManager.getFreeStage3DProxy();
stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
}
private function onContextCreated(event:Stage3DEvent):void
{
initAway3D();
initStarling();
var floor:Mesh = new Mesh(new PlaneGeometry(600, 400), new ColorMaterial(0x530000));
floor.mouseEnabled = true;
view3D.scene.addChild(floor);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
floor.addEventListener(MouseEvent3D.CLICK, onClick);
}
private function onClick(e:MouseEvent3D):void
{
trace("Click");
}
private function initAway3D():void
{
view3D.stage3DProxy = stage3DProxy;
view3D.shareContext = true;
addChild(view3D);
view3D.mousePicker = PickingType.SHADER;
view3D.camera = new Camera3D(new OrthographicLens());
view3D.camera.x = 1000;
view3D.camera.y = 1000;
view3D.camera.z = 1000;
view3D.camera.lookAt(new Vector3D(0, 0, 0));
}
private function initStarling():void
{
var starling:Starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.showStats = true;
starling.start();
}
private function onEnterFrame(e:Event):void
{
stage3DProxy.clear();
view3D.render();
stage3DProxy.present();
}
}
}
I'm working on a small project, I based it from this tutorial, and everything works fine!
I had a problem with feathers but I resolved it when I called starling.start() which is not in the tutorial, but you've already did it :)
Try this :
private function initStarling():void
{
starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.addEventListener(Event.ROOT_CREATED, rootCreatedHandler);
}
private function rootCreatedHandler(event:Event):void
{
starling.removeEventListener(Event.ROOT_CREATED, rootCreatedHandler);
stage3DProxy.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
starling.start();
}
private function onEnterFrame(event:flash.events.Event):void
{
view3D.render();
starling.nextFrame();
}
I hope that it will help you!

FLVPlayback stop/play

Problem with a FLVPlayback component and the stop() play() method. Here is the code of my class, videoPlayer is a FLVPlayback component on the stage. The array holds videos in the format [videos/s1.flv,videos/s2.flv,videos/s3.flv] When I call the stopVideo() function while the first video is still playing it stops and rewinds to frame 1 of the video (working as intended) but then when I call the playVideo() method it doesnt play the video again. It works when I stop the FLVPlayback while its playing the 2nd or 3rd video. I know the READY event isnt invoked at the 1st video because it was invoked at the beginning. What I'm doing wrong?
public class Intro extends MovieClip {
private var intros:XML;
private var currentVideo:uint = 0;
private var _data:XML;
private var videos:Array;
public function Intro(data:XML) {
_data = data;
videoPlayer.addEventListener(VideoEvent.COMPLETE, completeHandler);
videoPlayer.addEventListener(VideoEvent.READY, videoReady);
videoPlayer.opaqueBackground = 0x000000;
videoPlayer.autoRewind = true;
}
function completeHandler(evt:Event):void {
trace("video complete");
currentVideo++;
if (currentVideo < videos.length) {
videoPlayer.source = videos[currentVideo];
} else {
currentVideo = 0;
}
}
public function playVideo():void {
trace(currentVideo);
if (currentVideo == 0) {
trace(videos[currentVideo]);
videoPlayer.play(videos[currentVideo]);
} else {
videoPlayer.source = videos[currentVideo];
}
}
public function prepareVideos(introVideos:XML):void {
intros = introVideos;
var list:XMLList = intros.entry;
var entry:XML;
var len:int = list.length();
videos = new Array();
for (var i:int = 0; i < len; i++) {
entry = list[i];
videos.push(entry);
}
dispatchEvent(new Event(Event.COMPLETE));
}
public function stopVideo():void {
if (videoPlayer.playing) {
trace("video stopped");
videoPlayer.stop();
currentVideo = 0;
}
}
private function resetVideo(e:VideoEvent):void {
videoPlayer.seek(0);
}
private function videoReady(e:VideoEvent):void {
trace("video ready");
videoPlayer.play();
}
}
you only want to specificy the video name in the play method when first playing the video. after that (ie, after applying a stop() )just use the play() method with no parameter

Adding object to another sprite layer causes issues

Essentially, the spawning ships appear above the crosshair whereas I want it to be the other way around. I tried adding the crosshair to another layer but then clicking / 'shooting' the ships does nothing. Any ideas?
public class Main extends MovieClip {
public static var backgroundLayer:Sprite = new Sprite();
public static var gameLayer:Sprite = new Sprite();
public static var interfaceLayer:Sprite = new Sprite();
public static var menuLayer:Sprite = new Sprite();
public var mainMenu:menuMain = new menuMain();
public var intro:IntroSound = new IntroSound();
public var soundControl:SoundChannel = new SoundChannel();
public var crosshair:crosshair_mc;
static var enemyArray:Array = [];
private var enemyShipTimer:Timer;
private var enemyShipTimerMed:Timer;
private var enemyShipTimerSmall:Timer;
public function Main()
{
addMenuListeners();
addChild(gameLayer);
addChild(backgroundLayer);
addChild(interfaceLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
interfaceLayer.addChild(howtoPlay);
interfaceLayer.addChild(gameEnd);
interfaceLayer.addChild(gameAbout);
soundControl = intro.play(0, 100);
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function update(e:Event):void
{
for each (var enemy:EnemyShip in enemyArray)
{
enemy.update();
if (enemy.dead)
{
enemy.kill();
}
}
}
private function mouseDown(e:MouseEvent):void
{
if (e.target.name) {
switch (e.target.name) {
case "enemy_big":
updateScore(5);
e.target.parent.damage();
break;
case "enemy_medium":
updateScore(10);
e.target.parent.damage();
break;
case "enemy_small":
updateScore(15);
e.target.parent.damage();
break;
}
}
}
function addMenuListeners():void
{
//Code to add event listeners
}
public function startGame(e:Event)
{
removeMenuListeners();
soundControl.stop();
if (howtoPlay.parent == interfaceLayer)
{
interfaceLayer.removeChild(howtoPlay);
}
if (gameAbout.parent == interfaceLayer)
{
interfaceLayer.removeChild(gameAbout);
}
if (gameEnd.parent == interfaceLayer)
{
interfaceLayer.removeChild(gameEnd);
}
if (mainMenu.parent == menuLayer)
{
menuLayer.removeChild(mainMenu);
}
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function spawnEnemy(type:String, speed:Number) {
var enemy = new EnemyShip(type, speed);
enemyArray.push(enemy);
gameLayer.addChild(enemy);
return enemy;
}
function sendEnemy(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("big", Math.random() * 5 + 12);
}
function sendEnemyMed(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("medium", Math.random() * 7 + 14);
}
function sendEnemySmall(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("small", Math.random() * 9 + 16);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
scoreText.setTextFormat(scoreFormat);
}
static function removeEnemy(enemyShip:EnemyShip):void {
enemyArray.splice(enemyArray.indexOf(enemyShip), 1);
gameLayer.removeChild(enemyShip);
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
I expect MouseDowns are intercepted by the parent layer. You have employed mouseEnabled=false for crosshair, now you should do that for its parent.
public static var crosshairLayer:Sprite=new Sprite();
public function Main() {
... // initialize everything first
addChild(gameLayer);
addChild(crosshairLayer);
crosshairLayer.mouseEnabled=false;
crosshairLayer.mouseChildren=false;
}
Then you add crosshair to crosshairLayer.