Skip to main content
Inspiring
October 15, 2010
Question

Need help on how to embed YouTube video into Flash

  • October 15, 2010
  • 3 replies
  • 1555 views

This is my first try at this. This is how far I got.

I found a page on YouTube which should guide me through the process but I am not up to level on this.

I got this code which works and shows a blank YouTube player. I can not figure out where exactly and what code to put if I wish to show a video there:

// The player SWF file on www.youtube.com needs to communicate with your host

// SWF file. Your code must call Security.allowDomain() to allow this

// communication.

Security.allowDomain("www.youtube.com");

// This will hold the API player instance once it is initialized.

var player:Object;

var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);

loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));

function onLoaderInit(event:Event):void {

    addChild(loader);

    loader.content.addEventListener("onReady", onPlayerReady);

    loader.content.addEventListener("onError", onPlayerError);

    loader.content.addEventListener("onStateChange", onPlayerStateChange);

    loader.content.addEventListener("onPlaybackQualityChange",

        onVideoPlaybackQualityChange);

}

function onPlayerReady(event:Event):void {

    // Event.data contains the event parameter, which is the Player API ID

    trace("player ready:", Object(event).data);

    // Once this event has been dispatched by the player, we can use

    // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl

    // to load a particular YouTube video.

    player = loader.content;

    // Set appropriate player dimensions for your application

    player.setSize(480, 360);

}

function onPlayerError(event:Event):void {

    // Event.data contains the event parameter, which is the error code

    trace("player error:", Object(event).data);

}

function onPlayerStateChange(event:Event):void {

    // Event.data contains the event parameter, which is the new player state

    trace("player state:", Object(event).data);

}

function onVideoPlaybackQualityChange(event:Event):void {

    // Event.data contains the event parameter, which is the new video quality

    trace("video quality:", Object(event).data);

}

Here is a sample of a code which YouTube provides along with its videos for embeding:

<object width="960" height="745"><param name="movie" value="http://www.youtube.com/v/ihz-m2HZhvY?fs=1&hl=en_US&rel=0&color1=0x2b405b&color2=0x6b8ab6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ihz-m2HZhvY?fs=1&hl=en_US&rel=0&color1=0x2b405b&color2=0x6b8ab6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="960" height="745"></embed></object>

This topic has been closed for replies.

3 replies

Inspiring
October 17, 2010

There is nothing in your code that calls a video.

This code works for me - it plays video. Note line player.cueVideoById



function onPlayerReady(event:Event):void {
    // Event.data contains the event parameter, which is the Player API ID
    trace("player ready:", Object(event).data);
    // Once this event has been dispatched by the player, we can use
    // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
    // to load a particular YouTube video.
    player = loader.content;
    // Set appropriate player dimensions for your application
     player.setSize(480, 360)

     player.cueVideoById("W8ZunUae7mQ");
}

Goshine
Inspiring
October 17, 2010

If you have access to a serverside language like php that is

the best way  to beat the sandbox

October 15, 2010
System.security.allowDomain("http://www.youtube.com"); System.security.loadPolicyFile("http://www.youtube.com/crossdomain.xml"); this.createEmptyMovieClip("video_mc", 10); var mcl:MovieClipLoader = new MovieClipLoader(); var listener:Object = new Object(); listener.onLoadStart = function() {     trace("started"); }; listener.onLoadInit = function(mc) {     trace("loaded.");     _root.resizeInt = setInterval(checkResize, 10, mc, mc._width, {w:400,h:300}); }; listener.onLoadError = function() {     trace("error"); }; mcl.addListener(listener); mcl.loadClip("http://www.youtube.com/v/vGcnc2tgjZI", video_mc); function checkResize(vid:MovieClip, initW:Number, finalSize:Object) {     if (initW != vid._width) {         clearInterval(_root.resizeInt);         delete _root.resizeInt;         vid._width = finalSize.w;         vid._height = finalSize.h;         trace("\n\tresize applied - width:"+finalSize.w+", height:"+finalSize.h);     } }
That might work. I found it and didn't look at it so it could be way off...