Wednesday, September 23, 2009

Flash youtube nightmares.

So at work I’m working on a project that requires that youtube videos be embedded and controlled in an AS3 site. Unfortunately those lazy bastards at youtube still only have an AS2 chromeless player. This means that when you load it into your AS3 movie you can’t control it directly, you have to go through either local connection or javascript.

After a quick bit of research I decided on going the localConnection route, using TubeLoc, a nice and easy wrapper that loads in a ready made as2 swf and communicates using localConnection. Pretty simple and easy and I thought I had it cracked. Then we tested it around the office and found that it wouldn’t work on any of the office macs. It would work occasionally, but not reliably.

I got the tech department to set me up with a macbook myself so I could do some tests and research and found some pretty horrifying things. I found lots of people referring to localConnection not working correctly on Macs, especially since the last flash player update ( 10.0.32.18). Indeed, adobe did make a big change to how localConnection works on macs in that update, though they swear blind that it should be fully backwards compatible.

To test I made 2 swfs that use localConnection to simply send the contents of a text field back and forth. These worked perfectly on a PC, but on sporadically on the mac when I rebooted.

After this I concluded that I couldn’t trust localconnection, and so went over to the javascript method for the youtube api. This works, though it is a bit of a hassle, and it means that you can only test in a web-browser with the correct html and files.

If anyone has any suggestions I would love to hear them, otherwise I just wish Youtube would get off their arses and make an AS3 version of the player.

My localConnection test (switch LocalConnectionTest0 and LocalConnectionTest1 and save with different names - make sure you drag onto the browser as you standalone flash player probably won't be latest version):

package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.LocalConnection;
import flash.text.TextField;
import flash.text.TextFieldType;


public class LocalConnectionTest extends Sprite
{

private var receivingConnection :LocalConnection;
private var sendingConnection :LocalConnection;
private var inputTF :TextField;
private var outPuTF :TextField;

public function LocalConnectionTest()
{
receivingConnection = new LocalConnection();
receivingConnection.client = this;
receivingConnection.connect("LocalConnectionTest0");


sendingConnection = new LocalConnection();

inputTF = new TextField();
inputTF.type = TextFieldType.INPUT;
addChild(inputTF);
outPuTF = new TextField();
outPuTF.x = 200;
addChild(outPuTF);

var btn:Sprite = new Sprite();
addChild(btn);
btn.graphics.beginFill(0);
btn.graphics.drawCircle(100, 100, 20);
btn.addEventListener(MouseEvent.CLICK, send);
}

public function onLocalConnectionEvent(object_p:Object):void
{
inputTF.text = object_p.text;
}

public function send(evt:MouseEvent):void
{
sendingConnection.send("LocalConnectionTest1", "onLocalConnectionEvent", {text:inputTF.text});
inputTF.text = "";
}
}
}



No comments: