JSON encoding for Flash Player 11 and AIR 3.0 - json

I have the following code:
thinkGearSocket = new Socket();
var configuration : Object = new Object();
configuration["enableRawOutput"] = true;
configuration["format"] = "Json";
thinkGearSocket.connect("127.0.0.1", 13854);
thinkGearSocket.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
thinkGearSocket.writeUTFBytes(JSON.encode(configuration));
It works for flash player 10 but for flash player 11 I get an error saying:
1061: Call to a possibly undefined method encode though a reference with static type flash.net:socket
I had the same error for the decoding with this:
private function dataHandler(e : ProgressEvent){
//read data from the socket
var packetString : String = thinkGearSocket.readUTFBytes(thinkGearSocket.bytesAvailable);
thinkGearSocket.flush();
//split the data into an array
var packets : Array = packetString.split(/\r/);
var data:Object;
//iterate through array elements
for each (var packet:String in packets){
//sometimes the packet is empty
if(packet != "") {
try {
data = JSON.decode(packet);
//trace(packet);
} catch ( jError: JSONParseError) {
// do exception handling here
label1.text = jError.text;
}
But I changed:
data = JSON.decode(packet);
to:
data = JSON.parse(packet);
and now I don't get an error for that part. how do I fix the encoding part for Flash player 11 and AIR 3.0?

You need to use the stringify method instead of encode for Flash 11 or greater.
stringify(value:Object, replacer:* = null, space:* = null):String
You can see more information in the AS3 livedocs for stringify.

Related

GET request Flex / Actionscript 3

Struggling to find any documentation to do a simple GET request to a URL to return JSON in Actionscript / Flex.
Anyone know
Use the URLLoader API.
var myJSON :Object; //listing as per "key:" with "value"
var str_JSON: String = ""; //if you need text String not data Object
var siteloader :URLLoader = new URLLoader( new URLRequest( your_URL ) );
siteloader.addEventListener(Event.COMPLETE, whenSiteLoaded);
function whenSiteLoaded( evt :Event ) :void
{
myJSON = JSON.parse(evt.target.data); //get into Object
str_JSON = evt.target.data; //get into String
//... any other code
}
You should add to the above the listeners for error conditions (eg: a "file not found" situation)

WinRT MediaElement not working with InMemoryRandomAccessStream

We loaded video as bytes array, created InMemoryRandomAccessStream over this array and tried to MediaElement.SetSource. In UI we have message on MediaElement - Invalid Source. We tried to save this stream to file and read new stream from this file - works perfectly. Both stream are the identical (we check it using SequenceEqual).
What is the problem?
Part of our code:
var stream = await LoadStream();
mediaElement.SetSource(stream , #"video/mp4");
...
public async Task<IRandomAccessStream> LoadStream()
{
...
var writeStream = part.ParentFile.AccessStream.AsStreamForWrite();
foreach (var filePart in part.ParentFile.Parts)
{
writeStream.Write(filePart.Bytes, 0, filePart.Bytes.Length);
}
writeStream.Seek(0, SeekOrigin.Begin);
return part.ParentFile.AccessStream;
}
P.S - the mime-type is correct for sure
Thanks!

Accessing audio via function

In Unityscript I'm able to directly access audio data. In the scene, I have a gameobject with a sound file and a script attached to it.
var mySound : AudioClip;
mySound = audio.clip;
var mySoundChannels = mySound.channels;
However, I'm having problems trying to access audio data via a function:
#pragma strict
var mySound : AudioClip;
function Start()
{
mySound = audio.clip;
GetAudio(mySound);
}
function GetAudio(au)
{
print ("Audio: " + (mySound === au)); // true
//var mySoundChannels = mySound.channels; // works
var mySoundChannels = au.channels; // fails
var stereoOrNot = (mySound.channels == 2 ? "stereo" : " mono"); //works
print(stereoOrNot);
}
I thought I could access au.channels, but I'm not sure where I'm going wrong I (apart from wanting to access audio indirectly)
Since you are using a dynamic variable there, I'm not sure if the var mySoundChannels will be typed to an AudioClip or an int. If it is an AudioClip then it will fail because channels are read only. Try it with int mySoundChannels = au.channels;

How to get Facebook post id with Facebook-Actionscript SDK?

After I upload a photo on a desktop facebook application i need to store it's post id in a database. From the facebook ActionScript SDK documentation:
api() method
public static function api(method:String, callback:Function, params:* = null, requestMethod:String = GET):void
Makes a new request on the Facebook Graph API.
Parameters [...]
callback:Function — Method that will be called when this request is complete The handler must have the signature of callback(result:Object, fail:Object); On success, result will be the object data returned from Facebook. On fail, result will be null and fail will contain information about the error.
So I implemented my callback function as follows:
protected function handleUploadComplete(response:Object, fail:Object):void{
status = (response) ? 'Success' : 'Error';
var file:File = File.desktopDirectory.resolvePath("Log.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(response.toString());
stream.close();
}
The problem is that response or response.toString() both return "[object Object]", and I was hoping for something more like "id: somenumber".
What am I doing wrong?
If everything has worked the response will contain a property called id that is your post ID. Otherwise it will be null and the fail object will be populated.
protected function handleUploadComplete(response:Object, fail:Object):void
{
var status:Boolean = response != null;
if(status) var id:String = response.id;
//do whatever you want
}

Extracting metadata object from MediaElement (AS3)

I am attempting to retrieve metadata from a URLResource. The URLResource is not controlled by me, but passed into a function that I am using.
The URLResource is created like this:
var resource:URLResource = new URLResource("http://mediapm.edgesuite.net/osmf/content/test/logo_animated.flv");
// Add Metadata for the URLResource
var VideoParams:Object = {
Name:"Logo Video",
Owner:"Self",
Duration:"1:25:20",
category:"education"
}
resource.addMetadataValue("VideoParams", VideoParams);
var media:MediaElement = factory.createMediaElement(resource);
Now the URLResource contains the metadata. I will recieve a MediaElement resource. How do I extract the metadata back?
Here's what the debugger shows (media is a MediaElement object containing the URLResource w/ metadata) :
fdb>print media.resource.
$1 = [Object 246396705, class='org.osmf.media::URLResource']
_mediaType = null
_metadata = [Object 416970209, class='flash.utils::Dictionary']
_mimeType = null
url = "http://mediapm.edgesuite.net/osmf/content/test/logo_animated.flv"
fdb>print media.resource._metadata.
$2 = metadata = [Object 416970209, class='flash.utils::Dictionary']
VideoParams = [Object 416970305, class='Object']
(fdb)print media.resource._metadata.VideoParams.
$3 = VideoParams = [Object 416970305, class='Object']
category = "education"
Duration = "1:25:20"
Owner = "Self"
Name = "Logo Video"
I've attempted extracting the metadata object with:
media.resource.getMetadata("VideoParams");
and a host of other attempts, but can't figure out how to get at that darned metadata.
Any thoughts greatly appreciated!
This actually turned out to be pretty easy...just needed to use the getMetadataValue function in the URLResource object like this:
var temp:Object = media.resource.getMetadataValue("VideoParams");
trace('Owner:', temp.Owner);