Sound plays with latency in flash and air as3 - actionscript-3

I'm developing a piano app in as3; however, I get too much latency.
I am using short WAV files from my flash library (not streaming) and loading the files in the constructor. I tried also working with MP3 files but I get the same latency.
I understand that developers know about that and there are a lot of threads regarding it but no one really solves the issue.
Does anyone know a workaround in order to reduce the latency?
There is a solution that says to play a silent sound all the time to prevent the sound mixer going to sleep. I tried it and it didn't work (or maybe I did it wrong).
Here is the code, it's just a simple code to test the latency
package
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SoundLatencyMain extends Sprite
{
public var MainSoundChannel:SoundChannel;
public var playbutton:MovieClip;
public var TheSound:Sound;
public var bgmusic:Sound;
public var bgsoundboolean:Boolean;
public function PlayBGMusic(me:MouseEvent):void
{
MainSoundChannel = bgmusic.play();
}
public function PlaySound(me:MouseEvent):void
{
MainSoundChannel = TheSound.play();
}
public function SoundLatencyMain()
{
MainSoundChannel = new SoundChannel();
playbutton = new PlaybgMusic_MC();
playbutton.x = stage.stageWidth / 2;
playbutton.y = stage.stageHeight / 2;
addChild(playbutton);
playbutton.addEventListener(MouseEvent.CLICK, PlayBGMusic);
bgmusic = new BGMusic();
TheSound = new TestSound();
stage.addEventListener(MouseEvent.MOUSE_DOWN, PlaySound);
}
}

Related

ActionScript 3.0 sound not working

So having trouble making sound on keyboard press
I have the imports:
import flash.net.URLRequest;
import flash.media.Sound;
I have the variables
private var soundDownRequest:URLRequest = new URLRequest ("SoundDown.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
and the event listener
private function keyDownHandler(evt:KeyboardEvent):void
{
if (evt.keyCode == 40)//ascii for down arrow
{
downSound.play();
}
}
The sound folder is in the same folder as the .as, its also in the library of the fla, yet it still doesn't work. Any idea why?
Thank you.
Update:
I got the sound to work but not using the external method I was trying to do above.
Had to do it internally.
so you need:
import flash.media.SoundChannel;
-Then you need to make sure your sound file is in your fla library.
once its in the library
-Right click > properties
-Select the Action Script Tab
-Check "export for action script"
-Give the class a name in accordance to the sound
-press ok
add this variable (your will be different):
private var downSound:TheDownSound = new TheDownSound();
downsound is the selected name of the variable, and TheDownSound is the name of the class (the one made earlier for the sound file)
then add this to where you want the sound to play:
var myDownSound:SoundChannel = downSound.play();
Do this if you cant get it working externally like me.
for a better explanation watch this guys youtube video:
https://www.youtube.com/watch?v=SZpwppe7yGs
Your code is working perfectly ok if you put your .mp3 file in the same folder as the output .swf, not near the class .as source file (because its the swf file loading the sound, so the path must be relative to it)
public class ASEntryPoint extends Sprite {
private var soundDownRequest:URLRequest = new URLRequest ("click.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
public function ASEntryPoint() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(evt:KeyboardEvent):void{
if (evt.keyCode == 40) {
downSound.play();
}
}
}
You need to load the external file, which is asynchronous operation. Then you track the loading event and if it all goes normally you can play your loaded sound.
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
// Keep the sound loader from being garbage collected.
var soundLoader:Sound;
function loadSound(url:String):void
{
var aRequest:URLRequest = new URLRequest(url);
soundLoader = new Sound();
// Handle the normal loading.
soundLoader.addEventListener(Event.COMPLETE, onLoaded);
// Handle the error cases.
soundLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
soundLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
soundLoader.load(aRequest);
}
var audioChannel:SoundChannel;
function onLoaded(e:Event):void
{
// Sound is available here for playback.
audioChannel = soundLoader.play();
}
function onError(e:Event):void
{
trace(e);
}
You can also handle your sound as a streaming audio, but I worked with that years ago in AS2 so I cannot help here. Still, internet suggests a link: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d22.html

How to find out framerate of JW Player video on client computer

We need to display the framerate of a video that user is watching with JW Player. We have already tried tried everything with JW Player 7 API but it doesn't give us the client side frame rate.
We have built a JW Player plugin which is supposed to calculate the framerate on every ENTER_FRAME event while the video is playing.
We are listening to ENTER_FRAME event on line 61.
api.addEventListener(Event.ENTER_FRAME, calculateFPS);
Everytime a new frame is entered the plugin executes calculateFPS function. The framerate is displayed in a textbox.
For some reason the calculated framerate is around 30 fps. This cannot be possible because the original framerate of the video is 15 fps.
I think that the problem might be on line 61 where the eventlistener is listening to the player instead of the video. Is it possible to link the eventlistener straight to the video?
Here is the whole plugin actionscript code. It is based on a JW Player plugin example APITester.as.
package {
import com.longtailvideo.jwplayer.events.*;
import com.longtailvideo.jwplayer.player.*;
import com.longtailvideo.jwplayer.plugins.*;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.events.Event;
import flash.utils.getTimer;
/** An example plugin that tests various player integrations. **/
public class APITester extends Sprite implements IPlugin6 {
[Embed(source="mute.png")]
private const MuteIcon:Class;
private var api:IPlayer;
private var field:TextField;
private var clickButton:Sprite;
public var startTime:Number;
public var framesNumber:Number = 0;
public var fps:TextField = new TextField();
/** Let the player know what the name of your plugin is. **/
public function get id():String {
return "apitester";
};
public function get target():String {
return "6.0";
}
/** Constructor **/
public function APITester() {
clickButton = new Sprite();
clickButton.addChild(new MuteIcon());
clickButton.y = 10;
clickButton.buttonMode = true;
addChild(clickButton);
startTime = getTimer();
addChild(fps);
};
/** Called by the player after the plugin has been created. **/
public function initPlugin(player:IPlayer, config:PluginConfig):void {
api = player;
api.addEventListener(Event.ENTER_FRAME, calculateFPS);
api.play();
};
private function calculateFPS(evt:Event):void {
var currentTime:Number = (getTimer() - startTime) / 1000;
framesNumber++;
if (currentTime > 1)
{
fps.text = "FPS: " + (Math.floor((framesNumber/currentTime)*10.0)/10.0);
startTime = getTimer();
framesNumber = 0;
}
};
/** If the player resizes itself, this gets called (including on setup). **/
public function resize(wid:Number, hei:Number):void {
clickButton.x = wid - 50;
}
}
}

Gettling starling to work with box2d and debugdraw with as3

This topic has been raised before, but not one of the examples I've found online seem to work for me! I am trying to get starling to work with box2d, and also for the box2d debugdraw.
I have tried a bunch of different methods, and my code is now a bit of a mess due to commenting out to try different mixes of the "solution". Does anyone know how to do all this properly? I would be greatly in dept if someone could explain it.
Here is the last attempt I tried:
In my Startup class:
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
//import flash.events.Event;
import starling.core.Starling;
//import starling.display.Sprite;
import flash.events.Event;
[SWF(width="640", height="480", frameRate="60", backgroundColor="#000000")]
public class Startup extends Sprite {
public static var mStarling:Starling;
public static var debugSprite:Sprite;
public function Startup() {
//addChild ( new Stats() );
super();
//stage.align = StageAlign.TOP_LEFT;
//stage.scaleMode = StageScaleMode.NO_SCALE;
// create our Starling instance
mStarling = new Starling(Game, stage);
// set anti-aliasing (higher the better quality but slower performance)
mStarling.antiAliasing = 1;
mStarling.showStats = true;
// start it!
mStarling.start();
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
}
private function onContextCreated(e:Event):void{
////debug mode
// debugSprite=new Sprite();
// //addChild(debugSprite);
// Starling.current.nativeOverlay.addChild(debugSprite);
//var debugSprite:Sprite=new Sprite();
addChild(debugSprite);
(mStarling.stage.getChildAt(0) as Game).DebugDraw(debugSprite)
}
I call the debugdraw like this:
debugDraw(Startup.debugSprite);
Here is a heavily commented out debugdraw:
private function debugDraw(debugSprite:flash.display.Sprite):void {
/*var worldDebugDraw:b2DebugDraw=new b2DebugDraw();
//var debugSprite:flash.display.Sprite = new flash.display.Sprite();
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
//mStarling.current.nativeOverlay.addChild(debugSprite);
//worldDebugDraw.SetSprite(debugSprite);
//debugDraw.SetSprite(Starling.current.nativeOverlay); //DOESN'T SEEM TO WORK
worldDebugDraw.SetDrawScale(worldScale);
worldDebugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
//worldDebugDraw.SetFillAlpha(0.0);
//worldDebugDraw.SetAlpha(0.0);
//visible
worldDebugDraw.SetFillAlpha(0.8); //for testing
worldDebugDraw.SetAlpha(1); //for testing
world.SetDebugDraw(worldDebugDraw);*/
var worldDebugDraw:b2DebugDraw = new b2DebugDraw();
worldDebugDraw.SetSprite(debugSprite);
world.SetDebugDraw(worldDebugDraw);
}
DebudDraw works with ONE classic Flash Sprite (you keep creating new ones for some reason). Only one b2DebugDraw should be created and set with that ONE Sprite.
Starling pretends to own the stage and all displaylist but it doesn't. Use simply your StartUp instance or even directly the stage itself without passing through Starling it will avoid some confusion.
The right way to do all this:
public var debugSprite:Sprite;//no static var
Starting what?
//don't start it!
mStarling.start();
You star something that cannot be started. At this point you are trying to create a valid Context3D so be patient. Remove that line.
Now in onContextCreated:
mStarling.start();//now you can start
debugSprite = new Sprite();//create your sprite
addChild(debugSprite);
var game:Game = mStarling.stage.getChildAt(0) as Game;//Game? somebody is following a tutorial ...
if(game)
{
game.setDebug(debugSprite);//this is a new public method to create in Game
}
In Game in method setDebug (with one parameter Sprite):
var worldDebugDraw:b2DebugDraw = new b2DebugDraw();
worldDebugDraw.SetSprite(myspriteparameter);
world.SetDebugDraw(worldDebugDraw);
That's it, don't create new Sprite, don't create new debugdraw, you are good to go that's all you need.

AS3/AIR Mobile Click and/or touchevents on moving objects

few weeks ago i asked a question about a similair issue, but within the Starling Framework (Starling TouchEvent on Sprite)
Now, i am working on another mobile app/game with Away3d. I seem to have the same problem touching/tapping a moving sphere or box. When the sphere is not moving, i can just tap it exactly at its position, but when it rotates, the click-event doesnt fire very accurate.
this only happens on Mobile (tested on Android 4.2.2: Nexus7/Galaxy S2 and iOS: iPad2/iPad Retina)
package {
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.core.pick.PickingColliderType;
import away3d.core.pick.PickingType;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.events.MouseEvent3D;
import away3d.primitives.SphereGeometry;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;
[SWF(frameRate="60")]
public class Main extends Sprite {
private var _container:ObjectContainer3D;
private var _view:View3D;
private var _sphere:Mesh;
public function Main()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
}
private function onAdded_toStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, onResize_stage);
init3d();
}
private function onResize_stage(e:Event):void {
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
private function onEnter_frame(e:Event):void {
_container.rotationY += 1;
_view.render();
}
private function onClick_sphere(e:MouseEvent3D):void {
//reposition the sphere
var deg:Number = Math.random() * 360;
_sphere.x = 250 * Math.cos(deg);
_sphere.z = 250 * Math.sin(deg);
_sphere.y = 80 + (Math.random() * 40);
}
private function init3d():void {
//create the 3d-view
_view = new View3D();
_view.width = stage.stageWidth;
_view.height = stage.stageWidth;
addChild(_view);
//create a cub
_view.camera.z = -400;
_view.camera.y = 75;
_view.camera.lookAt(new Vector3D(0, 125, 0));
//create container
_container = new ObjectContainer3D();
_view.scene.addChild(_container);
//create sphere to tap
_sphere = new Mesh(new SphereGeometry(10));
_sphere.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
_sphere.y = 100;
_sphere.z = -250;
_sphere.mouseEnabled = true;
_sphere.addEventListener(MouseEvent3D.CLICK , onClick_sphere);
_container.addChild(_sphere);
var stats:AwayStats = new AwayStats(_view);
addChild(stats);
addEventListener(Event.ENTER_FRAME, onEnter_frame);
}
}
NOTE: i also tried different framerates, more events(Mouse_up, down etc), different pickers.
As this problem also happens with 2d moving sprites (in starling) my guess is that is might be Stage3D or AIR related, instead of starling/away3d.
Anyone has some suggestions? Maybe i'm forgetting to set something in the app.xml?
Both framework have to deal with an event less system (Stage3D). Stage3D doesn't catch or receive any events and certianly not touch or mouse event. So both frameworks simulate those events with an internal system that catches events on the stage and try to reproduce an accurate estimation. I personally find those systems too CPU expensive and not accurate enough so my advice is to do like me, create your own system. Simply catch event on the stage and decide which of displayed objects (on Stage3D using their position) has been touched. I personally get a performance boost out of it (especially with starling) and much more accuracy.

Embedding an SWF animation at a specific location in an AS3 project

I'm doing a project in AS3 (no external libraries or Flex) and I need to embed an explosion animation over a tile when it gets destroyed. I have a small explosion animation but I don't know how I can get it to play at a specific location. Is there a way to do this? I've tried embedding it like so:
[Embed(source="../assets/64x48.swf", mimeType="application/octet-stream")] private var Explosion:Class
var explosion:MovieClip;
explosion = new Explosion();
explosion.play();
but this doesn't seem to do anything. If not an SWF, I also have a sprite sheet of an explosion I could use, but I'm not sure how to animate a sprite without using an external library.
Approach within your swf Movieclip you want to play, you must perform the following. swf convert to bytearray after, it must be restored.
EDIT
I checked your swf file. however, your swf file contents not MovieClip, but AVM1Movie file. so It is quite difficult to directly embedded. because AVM1Movie file convert to MovieClip Algorithm. but don't worry. using a ForcibileLoader easily available.
download a ForcibileLoader
original ForcibileLoader here
refer a following code. I tested great work.
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.getTimer;
import org.libspark.utils.ForcibleLoader;
public class TestProject2 extends Sprite
{
private var loader:Loader = new Loader();
private var mc:MovieClip;
public function TestProjec2t()
{
var fLoader:ForcibleLoader = new ForcibleLoader(loader);
fLoader.load(new URLRequest("../asset/64x48.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
this.addChild(loader);
}
private function onSwfLoaded(e:Event):void
{
trace(e.currentTarget.content);
mc = e.currentTarget.content as MovieClip;
mc.x = Math.random() * stage.stageWidth;
mc.y = Math.random() * stage.stageHeight;
mc.gotoAndPlay(1);
}
}
}