Thursday, September 24, 2009

More Flash youtube fun



This works pretty well, though flex debugger seems to sometimes interfere and break it, making it a bit of a challenge to develop.

As it seems it has to be the case with youtube it mostly works, but not totally, the problem here being with the error handling. According to the documentation it should dispatch an event when the youtube video is either not found or is not available for embedding. It didn’t. After doing some checking and verifying that it wasn’t my code I opened up YoutubeLoader.as and found that there was nothing in there to handle any errors coming from the javascript. I then checked the javascript and found that there was also nothing in there to send or receive error messages from the as2 youtube player you’ve loaded in.

Simples, I think. I amended the javascript to catch the errors from the as2 player, then added in code in YoutubeLoader.as to catch the externalInterface calls. Feeling very smug I tested it out. I found that youtube are bollocks. A working youTube video always dispatches a 100 error, right at the start. This is the error for video not found. When video is not found it is also dispatches this error. There is no way of telling them apart. It does however send the 150/101 error correctly, which is the error for when the video is there but you’re not allowed to embed it.

So my solution is to keep in the error catching functions I added to catch when the video is there, but not allowed, then put in something that makes a request to the youtube api for the info on that video. If the ID is wrong or the video doesn’t exist then this will fail. I don’t like having to split my error handling up but it seems to be the only way.

Why youtube can’t get a measly couple of flash devs together to write a solid working player with no bugs or surprises is beyond me.

My changes to youtubeLoader.js:

function onYouTubePlayerReady(playerId) {
if (checkObj()) {
obj.addEventListener("onStateChange", "onytplayerStateChange");
obj.addEventListener("onError", "onytplayerError");
}
}
function onytplayerError(newState) {
//alert("Player's new state: " + newState);
obj.playerErrorHandler(newState);
}

My changes to youtubeLoader.as:

ExternalInterface.addCallback( "playerErrorHandler", playerErrorHandler);//Added to constructor
private function playerErrorHandler(errorCode:String):void
{
var e:YouTubeLoaderEvent = new YouTubeLoaderEvent(YouTubeLoaderEvent.IO_ERROR);
e.state = errorCode;
dispatchEvent(e);
}

No comments: