Dynamic Object Name in AS3 - actionscript-3

I have this code in my MXML project, I want to get dynamic Stream connection for different and dynamic usernames.
private var inStream:NetStream;
private function listenStream(user:String):void
{
this["inStream"+user] = new NetStream(connection);
this["inStream"+user].play(user);
}
private function closeStream(user:String):void
{
//clear stream listener
this["inStream"+user].close();
}
But this code is not worked, How can i build dynamic object names in ActionScript3?
Thanks alot

Try Dictionary
import flash.utils.Dictionary
private var streamDict:Dictionary = new Dictionary();
private function listenStream(user:String):void
{
var key:String = getKey(user);
var lastStream:NetStream = streamDict[key] as NetStream;
if (lastStream)
{
//close the last stream or do sth else
}
else
{
streamDict[key] = new NetStream(connection);
streamDict[key].play(user);
}
}
private function closeStream(user:String):void
{
var key:String = getKey(user);
//clear stream listener
var stream:NetStream = streamDict[key] as NetStream ;
if (stream)
{
stream.close();
}
//delete the stream
streamDict[key] = null;
delete streamDict[key];
}
private function getKey(user:String):String
{
return "inStream" + user;
}

Related

Actionscript 3 Video loop

I want to have embed video in flash game and have it looped. Is there a way to do it? As my code or more likely .seek or .resume dont give any effect? Am I using wrong commands or?
[Embed(source = "fast.flv", mimeType = "application/octet-stream")]
public var bytes:Class;
public var vidNS:NetStream
public var video:Video = new Video(1280, 720);
public var ns:NetStream;
public function Main() {
Doit();
}
private function Doit():void{
addChild(video);
var vidNC:NetConnection = new NetConnection(); vidNC.connect(null);
vidNS = new NetStream(vidNC);
var metaListener :Object = new Object(); metaListener = { onMetaData: process_Metadata };
vidNS.client = metaListener;
vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler);
vidNS.play(null);
var file:ByteArray = new bytes();
vidNS.appendBytes(file);
video.attachNetStream(vidNS);
}
function process_Metadata (in_Data :Object):void
{
trace("duration is : " + in_Data.duration );
}
function videoStatusHandler (event:NetStatusEvent):void
{
if (event.info.code == "NetStream.Buffer.Empty")
{
trace('loop')
vidNS.seek(0); vidNS.resume();
}
}
You can loop by just simply re-feeding the bytes to the decoder (NetStream)
It's possible that you also need to involve the option RESET_BEGIN:
yourNS.appendBytesAction( NetStreamAppendBytesAction.RESET_BEGIN );
Try setting your code like below:
[Embed(source = "fast.flv", mimeType = "application/octet-stream")]
public var bytes:Class;
public var vidNS:NetStream;
public var video:Video = new Video(1280, 720);
public var ns:NetStream;
//# declare these vars outside of a function for global access
public var file:ByteArray = new bytes();
public var vidNC:NetConnection;
public var metaListener :Object;
public function Main()
{
Doit();
}
private function Doit():void
{
addChild(video);
vidNC = new NetConnection(); vidNC.connect(null);
vidNS = new NetStream(vidNC);
metaListener new Object(); metaListener = { onMetaData: process_Metadata };
vidNS.client = metaListener;
vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler);
vidNS.play(null);
//var file:ByteArray = new bytes();
vidNS.appendBytes(file); //# feed video bytes to decoder
video.attachNetStream(vidNS);
}
function process_Metadata (in_Data :Object):void
{
trace("duration is : " + in_Data.duration );
}
function videoStatusHandler (event:NetStatusEvent):void
{
if (event.info.code == "NetStream.Buffer.Empty")
{
trace('now doing: looping...')
//# there is no timeline ".seek" in appendBytes mode, use ".seek" to clear the buffer for a new re-feed
vidNS.seek(0); //clears the current buffer (video data is removed)
vidNS.appendBytesAction( NetStreamAppendBytesAction.RESET_BEGIN ); //re-set timestamps for decoder
vidNS.appendBytes(file); //re-feed video bytes to decoder
}
}
So Thanks to VC. One i ended up with this code:
[Embed(source="1.flv", mimeType="application/octet-stream")]
public var bytes:Class;
public var vidNS:NetStream;
public var video:Video = new Video(1280, 720);
public var file:ByteArray = new bytes();
public var vidNC:NetConnection;
public var metaListener :Object;
public function Main()
{
Doit();
}
private function Doit():void
{
addChild(video);
vidNC = new NetConnection(); vidNC.connect(null);
vidNS = new NetStream(vidNC);
metaListener new Object(); metaListener = { onMetaData: process_Metadata };
vidNS.client = metaListener;
vidNS.play(null);
vidNS.appendBytes(file);
video.attachNetStream(vidNS);
}
function process_Metadata (in_Data :Object):void
{
trace("loop ");
vidNS.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
vidNS.appendBytes(file);
}
Its working like it should, but i'm not sure if thats the right way of doing it ^^.

Can not retrieve data from completely Loaded static variable AS3

Basically I was trying to read an image in binary with help of URLLoader() and convert it to a image again in flash.
I wrote 2 static methods that one of them read image and set data properties to binary and other one is the COMPLETE handler.
In COMPLETE handler I convert the binary data to ByteArray and assign it to a static variable. then tried to access to it from Main Timeline but it won't work.
The problem is my temporary variable not fully loading the static variable and when I'm checking BytesAvailable it's something like only 2.
What I'm doing wrong?
The COMPLETE handler:
public static function compHand(e:Event):void
{
var myByteArray:ByteArray = URLLoader(e.target).data as ByteArray;
LoaderToObject.ImageBytes = myByteArray;
trace(myByteArray.bytesAvailable);
if (LoaderToObject.ImageBytes.length != myByteArray.length)
{
trace(myByteArray.length);
trace(LoaderToObject.ImageBytes.length);
}
else
{
trace("successFully loaded!");
}
}
public static function loadBin(FileUrl:String)
{
var imageBytes:ByteArray=new ByteArray();
var binUrl:URLRequest = new URLRequest(FileUrl);
var binLoader:URLLoader = new URLLoader(binUrl);
binLoader.dataFormat = URLLoaderDataFormat.BINARY;
binLoader.addEventListener(Event.COMPLETE,compHand);
//trace(LoaderToObject.ImageBytes);
trace(FileUrl);
}
The function I used in main timeLine:
function loadfile(e:MouseEvent):void
{
LoaderToObject.loadBin("chick.bmp");//send file to URLLoader
imageBytes= LoaderToObject.ImageBytes;
MakeImage2(imageBytes);
}
Please help me
Because URLLoader is asynchronous, so When you try to access imageBytes, the imageBytes hasn't been load complete yet.
Your need to pass callback function to loadBin for been called when load complete
Here is an example
The load class
Class MyLoader {
private var callback:function;
private var binLoader:URLLoader;
public function load(FileUrl:String, $callback:Function):void
{
callback = $callback;
//your code
var imageBytes:ByteArray=new ByteArray();
var binUrl:URLRequest = new URLRequest(FileUrl);
binLoader = new URLLoader(binUrl);
binLoader.dataFormat = URLLoaderDataFormat.BINARY;
binLoader.addEventListener(Event.COMPLETE,compHand);
//trace(LoaderToObject.ImageBytes);
trace(FileUrl);
}
private function compHand(e:Event):void
{
var myByteArray:ByteArray = URLLoader(e.target).data as ByteArray;
if (callback != null)
{
callback(myByteArray, this);
}
}
}
LoaderToObject's load function
//make MyLoader class live, it's important
private var loadValidDic:Dictionary = new Dictionary();
public static function loadBin(FileUrl:String, callback:Function)
{
var loader:MyLoader = new MyLoader();
loadValidDic[loader] = callback;
loader.load(FileUrl, internalComplete);
}
private static function internalComplete(ba:ByteArray, loader:MyLoader):void
{
var callback:function = loadValidDic[loader];
if (callback)
{
callback(ba);
loadValidDic[loader] = null;
delete loadValidDic[loader];
}
}
Used in timeline
function loadfile(e:MouseEvent):void
{
LoaderToObject.loadBin("chick.bmp", onComplete);//send file to URLLoader
}
private function onComplete(imageBytes:ByteArray):void
{
MakeImage2(imageBytes);
}

Putting Urlloader in a Class

I have searched on google en different pages i found the problem but not the solution.
I have a class i made and that class is called WebServiceController:
public class WebServiceController
{
private var url:String;
private var post:Object=new Object();
private var loader:URLLoader = new URLLoader();
private var postVariable:String="";
private var getVariable:String="";
private var Geladen:Boolean=false;
public function WebServiceController()
{
}
public function postUrlData(u:String,p:Object):String
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
function onLoaderComplete(event:Event):void
{
return loader.data;
}
}
Now from my movieclip i call the next function but it says undefined:
var wb:WebServiceController = new WebServiceController();
trace(wb.postUrlData(url,post));
I dont know how to solve this. I tried different things but it keeps saying undefined.
The URLLoader.load call is executed asynchronously so the data has not been returned when you attempt to trace it out in the class that instantiates your WebServiceController class.
To access the data in the parent class, your best bet is probably to dispatch an event from the WebServiceController class when the data has loaded and catch it in the parent class.
WebServiceController:
public function postUrlData(u:String, p:Object):void
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}
private function onLoaderComplete(event:Event):void
{
// We can pass the event on like this
dispatchEvent(event);
}
Parent class:
public function initWebServiceController():void
{
var wb:WebServiceController = new WebServiceController();
wb.addEventListener(Event.COMPLETE, onWebServiceControllerDataLoaded);
wb.postUrlData(url, post);
}
private function onWebServiceControllerDataLoaded(event:Event):void
{
// The event target is the URLLoader instance. We can
// access the loaded data via its data property
trace(URLLoader(event.target).data);
}

Add complete contact object from Facebook to SqLite

I can't add name and image from Facebook contacts to SqLite, becouse adding is inside in loop and loader start when Event is Complete. Fllowing code adds data to the database, but in the name of the object is the last person out of the loop. Any thoughts? Thanks!
private var person:PersonVO;
protected function handleFriendsLoad(response:Object, fail:Object):void
{
if (fail) { return }
var friends:Array = response as Array;
var l:uint=friends.length;
for (var i:uint=0;i<l;i++) {
var friend:Object = friends[i];
FacebookDesktop.api('/'+friend.id, loadData);
}
}
private function loadData(object:Object, fail:Object):void
{
if (fail) { return; }
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageToByteArray);
loader.load(new URLRequest(FacebookDesktop.getImageUrl(object.id, 'large')));
person = new PersonVO()
person.name = object.first_name;
}
private function imageToByteArray(event:Event):void
{
var wczytaj:Loader = (event.target as LoaderInfo).loader;
var image:Bitmap = Bitmap(wczytaj.content);
var encoderJpeg:JPEGEncoder = new JPEGEncoder();
var byteArray:ByteArray = encoderJpeg.encode(image.bitmapData);
person.image= byteArray;
insert(person);
}
private function insert(person:PersonVO):void
{
dbStatement.text = "INSERT INTO person (name,image) values(:name,:jpeg)";
dbStatement.parameters[":name"] = person.name;
dbStatement.parameters[":jpeg"] = person.image;
dbStatement.execute();
}
Extend Loader class functionality:
dynamic class DynamicLoader extends Loader{}
Then store whatever you want in instances of DynamicLoader:
private function loadData(object:Object, fail:Object):void
{
if (fail) { return; }
var person:PersonVO = new PersonVO();
person.name = object.first_name;
var loader:DynamicLoader = new DynamicLoader();
loader.person = person;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageToByteArray);
loader.load(new URLRequest(FacebookDesktop.getImageUrl(object.id, 'large')));
}
private function imageToByteArray(event:Event):void
{
var wczytaj:DynamicLoader = (event.target as LoaderInfo).loader as DynamicLoader;
var image:Bitmap = Bitmap(wczytaj.content);
var encoderJpeg:JPEGEncoder = new JPEGEncoder();
var byteArray:ByteArray = encoderJpeg.encode(image.bitmapData);
var person:PersonVO = wczytaj.person;
person.image= byteArray;
insert(person);
}
I have not used private var person:PersonVO;. So if you are not using it in the other methods of your class, you could remove it completely.

How to run function only when parameter variable has been assigned?

I need a way to wait running the parseCSV command until the readFile event has updated the content of importData. I have seen a few things about custom event dispatchers but cannot quite figure out how to use them in my situation.
private var importData : String;
public function importFile(event:MouseEvent):void {
var data:String = chooseFile();
parseCSV(importData);
}
public function chooseFile ():String {
var filetype:FileFilter = new FileFilter("CSV Files(*.csv)","*.csv");
var file:File = File.userDirectory;
file.browseForOpen("Select CSV file to import", [filetype]);
file.addEventListener(Event.SELECT, readFile);
return importData;
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
filestream.close();
}
You'll either need to add some callbacks or add some event listeners. I prefer callbacks:
function importFile(...) {
choseFile(function(file:File) {
readFile(file, parseCSV);
});
}
function choseFile(callback:Function) {
...
file.addEventListener(Event.SELECT, function(event:Event) {
callback(File(event.target));
});
}
function readFile(file:File, callback:Function) {
var data = ... read data from file ....;
callback(data);
}
What about just adding the line in the readFile function?
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
parseCSV(importData);
filestream.close();
}
The command will be executed as soon as importData is set.
If you wish to the custom events route, you need to dispatch your own custom Event. Each Event has a type parameter which is just a string to identify it with. For example Event.CHANGE is the same as using "change".
static public const CUSTOM = "myCustomEvent";
public function someConstructor():void {
addEventListener(CUSTOM, onCustomEvent);
}
public function testDispatch():void{
dispatchEvent(new Event(CUSTOM));
}
private function onCustomEvent(e:Event):void{
trace("custom event Dispatched");
}
So you could try something like this.
public function importFile(event:MouseEvent):void {
addEventListener(CUSTOM, onImport);
var data:String = chooseFile();
}
private function onImport(e:Event):void {
parseCSV(importData);
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
dispatchEvent(new Event(CUSTOM));
filestream.close();
}