使用AS3 Adobe AIR加载YouTube视频

3

众所周知,Youtube的ActionScript API已经停止使用。因此,我寻找替代方案来在我的Adobe AIR应用程序中播放Youtube视频。

我测试了一些代码,并记录了每个代码的结果,如下所示:

1° 代码:

var hl:HTMLLoader = new HTMLLoader();
hl.width = 500;
hl.height = 300;
hl.addEventListener(Event.COMPLETE, complete); //event is dispatched, but with js errors
hl.load(new URLRequest("http://www.youtube.com/embed/XXXXX"));

结果:出现了“Object.freeze不是函数”的错误。
2° 代码:
var webView:StageWebView = new StageWebView();
webView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
webView.stage = stage;
webView.addEventListener(Event.COMPLETE, complete); //event is dispatched, but with js errors
webView.loadURL("http://www.youtube.com/embed/XXXXX");

结果: 出现了相同的错误'Object.freeze不是一个函数'。

第三段代码:

// changed 'useNative' param to true for StageWebView
var webView:StageWebView = new StageWebView(true);
// the same code of 2° Code

结果:没有错误,但页面没有完全加载,并且视频无法播放。
4° 代码:
// I've changed loadURL to loadString, for both methods...
var stringPage:String = '<html><head></head><body><iframe width="640" height="360" src="http://youtube.com/embed/XXXXXX" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></body></html>';
webView.loadString(stringPage);
// or
hl.loadString(stringPage);

结果:出现黑色矩形,但是视频无法加载。

5° 代码:

// I've tried use the AS3 API URL:
hl.load(new URLRequest("http://www.youtube.com/v/XXXXXX"));
// or
webView.loadURL("http://www.youtube.com/v/XXXXXX");
结果:出现“不再支持嵌入Flash视频”的消息,视频无法播放。

“怪奇物语”:
// the Youtube Page Video works!
webView.loadURL("http://www.youtube.com/watch?v=XXXXXX");
// or
hl.load(new URLResquest("http://www.youtube.com/watch?v=XXXXXX"));

问题是:有人知道如何在2019年用ActionScript 3.0加载YouTube视频吗?
1个回答

0

尝试基本的嵌入(没有autoplayallowAccelorometer等):

而不是混合使用"',相反,只需在需要时取消转义"引号。
例如:width=\"640\"会产生字符串width="640"

所以:

'<html><head></head><body><iframe width="640" ... '; 

//should be written as...
"<html><head></head><body><iframe width=\"640\" ... "; 

代码 4:(未测试)

// I've changed loadURL to loadString, for both methods...
var stringPage:String = "<html><head></head><body><iframe width=\"640\" height=\"360\" src=\"http://youtube.com/embed/XXXXXX\" frameborder=\"0\" </iframe></body></html>";
webView.loadString(stringPage);
// or
hl.loadString(stringPage);

我希望这个小细节能够修复问题,从而正确加载Youtube。

附言:
另一个选项是直接访问Youtube服务器上的MP4文件(跳过Youtube播放器嵌入)。

如果您有兴趣获取直接链接,请告诉我(您需要自己创建AS3播放器控件)。


这个不起作用...出现了相同的错误...你能向我展示如何从YouTube服务器访问mp4吗?可能需要使用NetStream、NetConnection和Video对象? - bio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接