views:

1166

answers:

2

I'm working on a flash web application (Actionscript 2.0) for my honours project but am having trouble embedding youtube videos. Basically the user selects symbols which queries the youtube api with certain tags depending on the symbols chosenand a random video is then picked from the first 30 videos. I have this working using the following code:

on (release) {
url="http://gdata.youtube.com/feeds/api/videos?q=danger+passion&orderby=published&start-index="+random(30)+"&max-results=1&v=2"
getURL(url);
}

but this just displays a webpage with a link to the youtube video.

This is the code I'll be using as the foundations for the player:

// create a MovieClip to load the player into
var ytplayer:MovieClip = _root.createEmptyMovieClip("ytplayer", 1);

// create a listener object for the MovieClipLoader to use
var ytPlayerLoaderListener:Object = {
onLoadInit: function() {
// When the player clip first loads, we start an interval to
// check for when the player is ready
loadInterval = setInterval(checkPlayerLoaded, 250);
}
};

var loadInterval:Number;


function checkPlayerLoaded():Void {
// once the player is ready, we can subscribe to events, or in the case of
// the chromeless player, we could load videos
if (ytplayer.isPlayerLoaded()) {
ytplayer.addEventListener("onStateChange", onPlayerStateChange);
ytplayer.addEventListener("onError", onPlayerError);
clearInterval(loadInterval);
}
}

function onPlayerStateChange(newState:Number) {
trace("New player state: "+ newState);
}

function onPlayerError(errorCode:Number) {
trace("An error occurred: "+ errorCode);
}

// create a MovieClipLoader to handle the loading of the player
var ytPlayerLoader:MovieClipLoader = new MovieClipLoader();
ytPlayerLoader.addListener(ytPlayerLoaderListener);

// load the player
ytPlayerLoader.loadClip("http://www.youtube.com/v/pv5zWaTEVkI", ytplayer);

can anyone help me on how to get the id of the video (for example: pv5zWaTEVkI) from the feed? Or how to send a query from flash which will return the video url/id as opposed to the url of a feed. Any help would be much appreciated as my hand in rather soon. Thanks

+1  A: 

What you are getting is not a webpage but an RSS feed.

If you just want the youtube video ID you could split the string you get by watch?v= and the video string should by the first characters upto a ' (comma)

href='http://www.youtube.com/watch?v=aFyfbcZqdck' /><link

You can practice by splitting this string.

Ólafur Waage
Thanks for answering so quickly. I kind of understand what you're getting at but it's getting the url of the video passed from the rss feed back to flash in the first place. Then I can extract the video id from it.
You can use LoadVars in actionscript to load the html into a string. And then parse it.
Ólafur Waage
Fantastic worked it out. Totally didn't know you could load the entire html of a webpage into a string. Managed to use String.split to extract the video id. Thank you so much for you help.
You're welcome :D
Ólafur Waage
A: 

Hi there, would you please explain (with samples) the way you work it out. Thank you !

TOM
@Tom This is not the way to ask a question please refer to the FAQ http://stackoverflow.com/faq
phwd