I followed this tutorial Loading Facebook Profile Picture Into Flash SWF Using Graph API to get the users profile picture to show in my flash game, but I'm having some issues.
import com.facebook.graph.Facebook;
import com.facebook.graph.data.FacebookSession;
import com.facebook.graph.net.FacebookRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
//place your APP ID here
const APP_ID:String = "app_id";
//Initialize facebbok library
Facebook.init(APP_ID, HandleLogin);
function HandleLogin(response:Object, fail:Object):void
{
if(Facebook.getSession().accessToken)
{
LoadMyInfo();
LoadMyPicture();
}
}
function LoadMyInfo():void
{
Facebook.api("/me", MyInfoLoaded);
}
function MyInfoLoaded(response:Object, fail:Object):void
{
nameTextField.text = response.name;
}
function LoadMyPicture():void
{
debug.text = "Entered loadMyPicture() function ";
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, MyPictureLoaded, false, 0, true);
var request:URLRequest = new URLRequest("facebookProxy.php");
var variables:URLVariables = new URLVariables();
variables.path = Facebook.getImageUrl(String(Facebook.getSession().uid), 'large');
debug.appendText(" \nvariables.path: " + String(variables.path));
request.data = variables;
debug.appendText(" \nrequest.data: " + String(request.data))
loader.load(request);
debug.appendText(" \n" + String(loader.load(request)));
}
function MyPictureLoaded(event:Event):void
{
debug.appendText(" \nEntering MyPictureLoaded");
var loader:Loader = event.target.loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, MyPictureLoaded);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, MyPictureLoadError);
var bitmap = Bitmap(loader.content);
bitmap.smoothing = true;
//Note: picturePlaceholder is just a blank movie clip on the Stage
bitmap.x = picturePlaceHolder.x;
bitmap.y = picturePlaceHolder.y;
bitmap.width = picturePlaceHolder.width;
bitmap.height = picturePlaceHolder.height;
picturePlaceHolder.addChild(bitmap);
debug.appendText(" \nBitmap is null: " + String(bitmap == null) +
" \nBitmap X: " + String(bitmap.x) + " Bitmap Y: " + String(bitmap.y) +
" \nBitmap width: " + String(bitmap.width) +
" \nBitmap height: " + String(bitmap.height))
}
function MyPictureLoadError(e:IOErrorEvent):void
{
debug.appendText("Loading Error");
}
This is what my code looks like after following the tutorials on the link above. With the debug I noticed that the function MyPictureLoaded is never called.
They never specified where I would call the function LoadMyPicture. I assumed it would go together with LoadMyInfo so I placed it there. They have a working example but no source code, so I'm hope someone here would shed some light on this.
EDIT 1
Here is what it looks like when I put it up on facebook. The last line debug.appendText(" \n" + String(loader.load(request))); is outputting undefined. I also made a change in the variables.path = Facebook.getImageUrl(String(Facebook.getSession().uid), 'large'); I added (String(), 'large') because in the docs it saying to pass strings into there. Edit: I realized adding String() is pointless.
var request:URLRequest = new URLRequest("facebookProxy.php");
Did you do the part of the tutorial that writes a server-side PHP proxy loader on your server?
If you don't have a file named facebookProxy.php on your server where you run this it won't work (the code is requesting "./facebookProxy.php" which probably doesn't exist). If you want to run this in the debugger on your local machine, you will need to make that file in the same directory as your swf and make sure you have apache/php running on your localhost.
If that's too much work for you, I would suggest following some of the examples from the API' project page: http://code.google.com/p/facebook-actionscript-api/downloads/detail?name=GraphAPI_Examples_1_6.zip&can=2&q=
Related
I used the NativeProcess to run my .cmd file to use the convert of Imagemagick on Windows.
the cmd is
convert -resize 1%x2% 3% 4%
On my computer,it looked normal.
But on some other computer,the bug looked like the “-resize” was be garbled.
then I changed the cmd
convert 1% 2%x3% 4% 5%
but on some computer,it still have the bug.
Is that the air output UTF-8,and the windows CMD use Unicode?
How to use it will not produce this error?
my code is
private var mProcess:NativeProcess;
private function convertImgToTargetPng(filePath:String,resultFilePath:String,width:Number,height:Number,scale:Number=1):void
{
var convertFile:File = File.applicationDirectory.resolvePath("script").resolvePath("convert.cmd");
var nativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = convertFile;
nativeProcessStartupInfo.workingDirectory = File.userDirectory;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(width*scale);
processArgs.push(height*scale);
processArgs.push(filePath);
processArgs.push(resultFilePath);
nativeProcessStartupInfo.arguments = processArgs;
mProcess = new NativeProcess();
mProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.addEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
try
{
mProcess.start(nativeProcessStartupInfo);
}
catch (e : Error)
{
Alert.show("convert failed: " + e.message);
}
}
private function onConvertErrorHandler(event : ProgressEvent):void
{
var data:String = mProcess.standardError.readUTFBytes(mProcess.standardError.bytesAvailable);
Alert.show("convert error :" + data);
}
private function onConvertExitHandler(event : NativeProcessExitEvent):void
{
mProcess.removeEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.removeEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
Alert.show("convert success");
}
Untested but you can try...
Make a changed CMD file that looks like: convert 1% 2% 3% 4%
and update your AS3 code section to look like this...
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(String ( String(width*scale) + "x" + String(height*scale) ) );
processArgs.push(filePath);
processArgs.push(resultFilePath);
Let me now how it goes.
Also instead of a .cmd file, why not just run the process with convert.exe file itself? :
If that convert.exe is located here : c:\program files\imagemagick\convert.exe Then use path location in AS3 code as c:\\program files\\imagemagick\\convert.exe
private function convertImgToTargetPng(filePath:String,resultFilePath:String,width:Number,height:Number,scale:Number=1):void
{
//# Use your location of convert.exe
var convertFile : File = new File("C:\\imageMagick\\converter.exe");
if ( convertFile.exists ) //# If found in your location
{
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); //vc
nativeProcessStartupInfo.executable = convertFile;
nativeProcessStartupInfo.workingDirectory = File.userDirectory;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(String ( String(width*scale) + "x" + String(height*scale) ) );
processArgs.push(filePath);
processArgs.push(resultFilePath);
nativeProcessStartupInfo.arguments = processArgs;
mProcess = new NativeProcess();
mProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.addEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
try { mProcess.start(nativeProcessStartupInfo); }
catch (e : Error)
{ Alert.show("convert failed: " + e.message); }
}
}
i am attempting (for some time now) to upload a video to Facebook from an air for android app. i have recorded the video on the device, saved it to the device and have the path to the video stored in a string. i was advised to convert the video to byteArray and pass that into the FacebookMobile.uploadVideo method of the as3 Facebook api which i attempted to do but am getting a 353 You must select a video file to upload. error (from Facebook i presume).
file to byteArray
public function UICompleteHandler(event:MediaEvent):void
{
trace("Welcome back from the camera");
var media:MediaPromise = event.data;
trace("file info "+media.file.url + " - " + media.relativePath + " - " + media.mediaType);
filePath = media.file.url;
trace("Object encoding is: " + inBytes.objectEncoding + "\n\n" + "order file: \n\n");
readFileIntoByteArray(filePath, inBytes);
trace("length 1: "+inBytes.length);
trace("position 1: "+inBytes.position);
inBytes.position = 0; // reset position to beginning
inBytes.uncompress(CompressionAlgorithm.DEFLATE);
trace("position 2: "+inBytes.position);
inBytes.position = 0; //reset position to beginning
}
private function readFileIntoByteArray(fileName:String, data:ByteArray):void
{
var inFile:File = new File(fileName);
trace ("file to byte array "+ inFile.url);
trace ("file name var : "+fileName);
inStream.open(inFile , FileMode.READ);
inStream.readBytes(data);
inStream.close();
}
Upload to Facebook(attempt)
public function handleUpload(ev:TouchEvent)
{
trace ("posting to facebook - FileName: "+ accessCamera.fileName + " - FilePath: " + accessCamera.filePath);
var params:Object ={
title:'test upload on FB api',
description:'test upload on FB api',
fileName: accessCamera.fileName,
video: accessCamera.inBytes
}
//trace ("params.video = "+params.video);
FacebookMobile.uploadVideo('me/videos', onComplete, params);
}
private function onComplete( result:Object, fail:Object ):void
{
trace("facebook post onComplete called" );
if (result)
{
//result.id is id of post that was just posted
trace ("great");
}
else if (fail)
{
trace("post Failed");
trace('code: '+fail.error.code);
trace('message: '+fail.error.message);
trace('type: '+fail.error.type);
}
}
when i uncomment the trace params.video i get a huge amount of random characters which i thought to be the data of the byteArray
any guidance or help from anyone would be appreciated
i am attempting to upload a video to facebook using the as3 api(FacebookMobile.uploadVideo) which accepts a video as either fieleReferance or byteArray. a followed the example # http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d54.html for creating the byteArray i ma getting a 2058 error when uncompressing the byte array (this may be because i dident compress it but neither did adobe in the example).
if i comment this bit out i am getting an #353 You must select a video file to upload error from Facebook.
Byte Array Code:
public function UICompleteHandler(event:MediaEvent):void
{
trace("Welcome back from the camera");
var media:MediaPromise = event.data;
trace("file info "+media.file.url + " - " + media.relativePath + " - " + media.mediaType);
filePath = media.file.url;
trace("Object encoding is: " + inBytes.objectEncoding + "\n\n" + "order file: \n\n");
readFileIntoByteArray(filePath, inBytes);
trace("length 1: "+inBytes.length);
trace("position 1: "+inBytes.position);
inBytes.position = 0; // reset position to beginning
//inBytes.uncompress(CompressionAlgorithm.DEFLATE);
trace("position 2: "+inBytes.position);
inBytes.position = 0; //reset position to beginning
}
private function readFileIntoByteArray(fileName:String, data:ByteArray):void
{
var inFile:File = new File(fileName);
trace ("file to byte array "+ inFile.url);
trace ("file name var : "+fileName);
inStream.open(inFile , FileMode.READ);
inStream.readBytes(data);
inStream.close();
}
Handle Upload Code:
public function handleUpload(ev:TouchEvent)
{
trace ("posting to facebook - FileName: "+ accessCamera.fileName + " - FilePath: " + accessCamera.filePath);
var params:Object ={
title:'test upload on FB api',
description:'test upload on FB api',
fileName: accessCamera.fileName,
video: accessCamera.inBytes
}
trace ("params.video = "+params.video);
FacebookMobile.uploadVideo('me/videos', onComplete, params);
}
private function onComplete( result:Object, fail:Object ):void {
trace("facebook post onComplete called" );
if (result)
{
//result.id is id of post that was just posted
trace ("great");
}
else if (fail)
{
trace("post Failed");
trace('code: '+fail.error.code);
trace('message: '+fail.error.message);
trace('type: '+fail.error.type);
}
}
when i trace out the params.video i get a load of random characters. the problem seems to be facebook dosnt appear to see the bytearray(if i even success fully created it) as a video.
Trying to adapt this example given here
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00002413.html
Getting Error
1046: Type was not found or was not a compile time constant void.
import mx.video.*;
var listenerObject:Object = new Object();
listenerObject.metadataReceived = function(eventObject:Object):Void {
trace("canSeekToEnd is " + rmys01.metadata.canSeekToEnd);
trace("Number of cue points is " + rmys01.metadata.cuePoints.length);
trace("Frame rate is " + rmys01.metadata.framerate);
trace("Height is " + rmys01.metadata.height);
trace("Width is " + rmys01.metadata.width);
trace("Duration is " + rmys01.metadata.duration + " seconds");
};
rmys01.addEventListener("metadataReceived", listenerObject);
rmys01.contentPath = "rhym01.flv";
var vidsiz:TextField = new TextField();
vidsiz.text = "Video Size " + (rmys01.metadata.width) + " by " + (rmys01.metadata.height);
vidsiz.width = 550;
vidsiz.height = 700;
vidsiz.x = 450;
vidsiz.y = 300;
addChild(vidsiz);
im trying the read the actual Dimension of the flv video file.
In the example it's written as a comment in the beginning:
/**
Requires:
- FLVPlayback component on the Stage with an instance name of my_FLVPlybk
*/
I assume you are missing this and you don't have the component on stage with instance name 'rmys01', as I don't see you declaring it anywhere. Otherwise looks fine.
import mx.video.*;
So, do you work on AS3 project or AS2?
For AS3 project you should adapt this Tutorial.
I load an XML file with this simple Flash CS5 / ActionScript 3 program:
import flash.net.*;
var URL_REQUEST:URLRequest = new URLRequest('http://preferans.de/top-xml.php');
var URL_STREAM:URLStream = new URLStream();
var URL_VARS:URLVariables = new URLVariables();
var UPDATE_TIMER:Timer = new Timer(1000);
stop();
UPDATE_TIMER.addEventListener(TimerEvent.TIMER, handleTimer);
UPDATE_TIMER.start();
URL_REQUEST.method = URLRequestMethod.GET;
URL_REQUEST.data = URL_VARS;
URL_STREAM.addEventListener(IOErrorEvent.IO_ERROR, handleUserError);
URL_STREAM.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleUserError);
URL_STREAM.addEventListener(Event.OPEN, handleUserOpen);
URL_STREAM.addEventListener(ProgressEvent.PROGRESS, handleUserData);
URL_STREAM.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleUserStatus);
URL_STREAM.addEventListener(Event.COMPLETE, handleUserComplete);
URL_STREAM.load(URL_REQUEST);
function handleUserOpen(event:Event):void {
trace('handleUserOpen: ' + event);
}
function handleUserData(event:Event):void {
trace('handleUserData: ' + event);
}
function handleUserStatus(event:HTTPStatusEvent):void {
trace('handleUserStatus: ' + event.status);
}
function handleUserError(event:Event):void {
trace('handleUserError: ' + event);
}
function handleUserComplete(event:Event):void {
trace('handleUserComplete: ' + event);
try {
var str:String = URL_STREAM.readUTFBytes(URL_STREAM.bytesAvailable);
var xml:XML = new XML(str);
trace(xml);
} catch(e:Error){
trace('Invalid data: ' + e);
return;
}
}
function handleTimer(event:TimerEvent):void {
var now:int = getTimer();
trace(UPDATE_TIMER.currentCount + ' ' + now + ' ' + URL_STREAM.connected);
}
it works fine and I can see the XML content:
handleUserOpen: [Event type="open" bubbles=false cancelable=false eventPhase=2]
handleUserData: [ProgressEvent type="progress" bubbles=false cancelable=false eventPhase=2 bytesLoaded=2390 bytesTotal=2390]
handleUserStatus: 200
handleUserComplete: [Event type="complete" bubbles=false cancelable=false eventPhase=2]
<pref>
[ .... XML content .....]
</pref>
1 1054 true
2 2054 true
3 3054 true
4 4054 true
5 5054 true
.....
90 90054 true
91 91054 true
but I don't understand, why is URLStream.connected always true.
I even restart Apache at my web server, but it doesn't change anything.
I'm asking this question, because I plan implementing Comet-like (aka HTTP-push) calls in my program and need to know, if URLStream is still working/busy or if it is completed/interrupted and can be reused for a new load() call (and I don't want to introduce a workaround state variable for that).
Thank you!
Alex
I think with URLStream as long it accesses the file its connection stays established, even when it hasn't started downloading anything or has finished downloading something. So I think you have to .close() it manually in the .COMPLETE function after your done reading the values.