user1892540
user1892540

Reputation: 211

Embedding youtube into flash with actionscript 3

I'm struggling to embed a youtube video into my flash project, I just can't get anything to work after hours and hours of trying to find a working tutorial and studying the api google have provided. I want to have a player that could possibly play from a youtube playlist as the goal is to showcase live performances of a band, but I don't have a clue how to get it off of the ground!

If anyone can help, it would be greatly appreciated.

Thank you! :)

Upvotes: 0

Views: 3931

Answers (1)

Ribs
Ribs

Reputation: 1505

import flash.system.Security;

Security.allowDomain( '*' );
Security.allowInsecureDomain( '*' );

var vPlayer:Object;
var playerLoader:Loader;

function loadVideo():void
{
    playerLoader = new Loader();

    // next line loads a youtube player with no UI
    playerLoader.load( new URLRequest( 'http://www.youtube.com/apiplayer?version=3' ) );

    // wait for it to load
    playerLoader.contentLoaderInfo.addEventListener( Event.INIT, onLoaderInit );
}

function onLoaderInit( evt:Event ):void
{
    // 'vPlayer_container' is a movieclip on stage that you need to create to hold the youtube player.
    // add your youtube Loader, ( which is actually the player ), to vPlayer_container's display list.
    vPlayer_container.addChild(playerLoader);

    // set the vPlayer variable to be the loaded youtube player
    vPlayer = playerLoader.content;

    // wait for it to be ready
    vPlayer.addEventListener( 'onReady', onPlayerReady );
}

function onPlayerReady( evt:Event ):void
{
    vPlayer.removeEventListener( 'onReady', onPlayerReady );

    // set listener for onComplete and play/pause events
    vPlayer.addEventListener( 'onStateChange', onPlayerStateChange );

    // mute it on start if you want
    vPlayer.mute();

    // set size of video screen
    vPlayer.setSize( 392,220 );

    // now load your youtube video in your new youtube player
    // get this video number off the url to your youtube video
    vPlayer.loadVideoById( 'GEghz32qhiA', 0 );
}

function onPlayerStateChange( evt:Event ):void
{
    // if video is over
    if( Object(evt).data == 0 )
    {
        //do something when video is over
    }
}

// other player commands available - you need to make your own buttons for these
// vPlayer.mute();
// vPlayer.unMute();
// vPlayer.pauseVideo();
// vPlayer.playVideo();

// to start the whole process, call loadVideo();

Upvotes: 1

Related Questions