Recently the web got at least one more layer in the spaghetti of sites we are used to. Since the rising of the social networks and the User Generated Content sites everyone is providing the users with tools to embed parts of their web site in the user's blog, profile or other personalized web space. Now instead of many sites operating in solitude we now have one web page that includes information from many web sources.
A more recent development was the rich media, Images and video mainly. Most video sites now allow their users to embed their videos everywhere (except of MySpace... but that is another story that will probably have it's own post.). How can the video site owner know when someone is playing his video on some blog or profile? Remember that most of the video sites business model are commercial-based, and the commercials should run along-side with the video content, So targeting your embedded audience even just for GeoLocation can be worth a lot.
Most video players are written in flash, and the communication between Flash and web server never was that amazing. There are several things you need to do in order to have a Flash application report to a web server the Url of the page it is running inside.
First you need to use an ActionScript command called ExecuteExternalCall to call a javascript that reports the Page Url with the property document.location.href.
Set the AllowScripts attribute of the embed tag to "Always", and make sure that the hosting site allows those scripts to run. MySpace doesn't, so do try to execute any Javascript in any form on MySpace.
this will take you to a point where everything works fine in FireFox but no information received from explorer browsers. To allow this info to run through IE you need to add the Flash ClassId attribute to the embed tag, and remove the Type attribute (should be there before, with Flash/Application or something like that in it's value). At this point we got the Url from IE as well but our application stoped working.
We are still resolving this issue, and we might try sending Ajax request to the server straight from the "ExecuteExternalApplication" function, because we realized that the function call works fine, only getting the value back in ActionScript fails.
I'll update on this when we have more info, also I will appreciate any comments on how to handle such a trick.
benny.PublishPost();
Wednesday, June 13, 2007
Reporting Page Url from Flash video player = Monitization
Posted by
Benyamin Shoham
at
12:10 PM
View Comments
Friday, May 18, 2007
Using JSON to connect Flash with Asp.Net
Life of a programmer are becoming more and more simple when it comes to finding professional help online. Almost any question i have regarding my work, like how to do this, why that doesn't work, etc' I can always find the answer one google search away. Few clicks later i have an explenation, code sample or other solutions. This week i tried to do something that had no good samples or documentation. That made me feel like i have reached the limits of existing technology, and know i'm trying things that other never did before. It is quite exciting for me.
This week Alexey and I wrote a simple "Invite a friend" feature. There is nothing exciting about that in itself. The feature was written inside a Flash control and the business object that actually sends emails is part of our site's engine written in Asp.Net.
Http communication between Flash and web pages is common, and can be done by using the LoadVars object in ActionScript code. You can see a good example here. There are practicaly 3 ways to transfer data over Http to Flash objects. One is sending the data "as is", which is good when you pass one parameter or two. Two is using XML to pack the data on one side and unpack it on the other side. Three is using JSON as the data structure standard. I chose JSON because the code required to Serialize and Deserialize JSON objects and strings is much simpler, shorter, more intuitive and more object oriented. You will see in the samples that one line of code is required in every side of the interaction to persist objects over http between Asp.Net and Flash ActionScript, or any other two sides for that matter.
The c# code for creating Json object on the server is available here and uses Newtonsoft Json.Net objects.
The Actionscript code however, is not available on the internet yet, only here. this code recieves the Json object inside Flash and reads it's values like this: ( I got this code from Trannie who wrote the Json ActionScript 2 object.)
In my projects I used LoadVars to grab the JSON string, so my JSON file
had a NAME=VALUE format with VALUE being the JSON string:
client= {
"name" : "Client",
"crop" : { "x1" : 3, "y1" : 3, "x2" : 623, "y2" : 472 },
"link" : {
"type" : "site",
"url" : " http://www.clientsite.com"
},
"title" : "Job Name",
"text" : "This is a string describing the project",
"details" : [
{
"full": "Browse.jpg",
"thumb" : "THB_Browse.jpg"
},
{
"full": "HSWebShot2.jpg",
"thumb" : "THB_HSWebShot2.jpg"
}
]
}
Hopefully that is still valid JSON. It's an excerpt from a JSON file,
so I may have introduced some typos..
In AS, I'd do something like:
var client:Object;
var json = new JSON();
var lv:LoadVars = new LoadVars();
lv.onLoad = function(success) {
if(success) {
client = json.parse(this.client);
trace(client.name );
trace(client.details[1]);
getURL(client.link.url);
}
}
We had some trouble on the ActionScript side because we confused the LoadVars Http object with the internal Json object, so let me elaborate on this. the LoadVars object is the one that use Load method to send Http request to the server, and the onLoad callback function that recieves the response from the server. In order to send Json string in such a request you should use ActionScript syntax to wrap your Json string. The response stream should include the "client =" part, which is NOT generated by the Json server-side class. This part is actually not part of Json, but is used by the Flash LoadVars object in order to read this Json string in ActionScript.
Finally, i want to hear from you what kind of uses can you see for such technology that connects Flash applications with Asp.Net objects so smoothly and easily.
Until next time,
Benny.Serialize(this)
Posted by
Benyamin Shoham
at
4:00 AM
View Comments
Thursday, April 12, 2007
Flash object fails to call Javascript function
The most popular way to show video in web pages today is to play the video files inside flash players that are embedded inside the html page. Like everyone else we did it too and it works just fine.
A new feature that we added to our site is the ability to embed our flash player into any blog or site page, and for this purpose we also added a feature that sends information from the flash player to our server each time someone plays one of our videos somewhere out there. That way we can count how many views our videos get even outside the site.
When we added this view-counter feature we could not get it to work. The flash player did not reach the server page even though we wrote the calling code properly.
After looking at some professional blogs and sites we found out the problem: we use the name "5minPlayer" as our player tag name, and the Object tag that holds the flash player should not begin with a number...
There are other limitations to this object tag, and also many things you should know if you are going to use flash on your page, especially if you are implementing extensive interaction between flash and the html page. A great page on this issue can be found here.
Posted by
Benyamin Shoham
at
1:23 AM
View Comments
