Play a youtube video inside an AIR app (AS3)

2020-03-30 07:27发布

I'd like to know if it's possible to play a youtube video inside an AIR app using AS3 code.

It seems that youtube API is deprecated...

If anybody knows a way or a good tutorial.

Thx ----------------EDIT

I've tried this code :

var wt:uint;
var ht:uint ;
var webview:StageWebView ;
var rect:Rectangle;
var url:String;

url = "https://www.youtube.com/watch?v=hFupHx5G44s";

trace(url);

wt = this.stage.stageWidth;
ht = this.stage.stageHeight;

webview = new StageWebView();
rect = new Rectangle(0,300,wt,ht/2);

webview.stage = this.stage;
webview.viewPort = rect;
webview.loadURL(url);

But it's loading the all page of youtube (youtube video + the recommended videos..) with a scroller on the right. I'd like to load only the video (not all the youtube page).

2条回答
劫难
2楼-- · 2020-03-30 08:06
Emotional °昔
3楼-- · 2020-03-30 08:10

Edit :

I'd like to load only the video (not all the youtube page).

Use this link format :

url = "https://www.youtube.com/v/hFupHx5G44s";

for example, choose a format like below :
https://www.youtube.com/v/hFupHx5G44s - use the /v/ to get a SWF version https://www.youtube.com/embed/hFupHx5G44s - use the /embed/ to get a HTML5 version

It's for an ANDROID AIR app.

You should have mentioned that detail in the question. You could try adding

Security.allowDomain("www.youtube.com");
Security.loadPolicyFile("https://www.youtube.com/crossdomain.xml");


//////////# End of Edit


Why not use stageWebView to load the embed (HTML5) player as web page?

The YouTube AS3 API being deprecated likely means you cannot create your own user interface or use features like search. You can re-create the search function easily though (load "search results" source into String and extract links by parsing the source code).

Anyways, I just tried this code below and it worked for an AIR Desktop App.
Maybe you can use it as a starting point...

package  
{

import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;

import flash.events.*;
import flash.system.*;


public class Youtube_AIR_code extends MovieClip 
{
    public var YT_Loader : Loader;
    public var my_VIDEO_ID : String = "hFupHx5G44s"; //# the YouTube ID

    public function Youtube_AIR_code () 
    {
        Security.loadPolicyFile("http://www.youtube.com/crossdomain.xml");

        YT_Loader = new Loader();
        YT_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, YT_Ready );
        YT_Loader.load( new URLRequest("http://www.youtube.com/v/" + my_VIDEO_ID) );

    }

    function YT_Ready (e:Event) : void
    {
        trace("YouTube SWF :: [status] :: Loading Completed");

        //addChild(YT_Loader);

        //# Wait for Youtube Player to initialise
        YT_Loader.content.addEventListener("onReady", on_YT_PlayerLoaded );

    }

    private function on_YT_PlayerLoaded (e:Event) : void
    {
        //# Check Original Width/Height - Scale it proportionally               
        trace( "YT_Loader content Width  : " + YT_Loader.content.width );
        trace( "YT_Loader content Height : " + YT_Loader.content.height );

        //# Testing changed size and position
        YT_Loader.width = 320; YT_Loader.height = 240;
        YT_Loader.x = 30; YT_Loader.y = 100;

        addChild(YT_Loader); //# can add to stage only

    }

} //# End Public Class

} //# End Package
查看更多
登录 后发表回答