使用 Youtube AS3 Player API 时如何避免安全错误 [英] How to avoid Security Errors when using the Youtube AS3 Player API
问题描述
我正在使用 Youtube AS3 Player API 在 Flash 项目中加载视频.加载播放器 swf 时出现这个非常烦人的错误:
I'm using the Youtube AS3 Player API to load video's in a Flash Project. I get this really annoying Error when loading the Player swf:
SecurityError: Error #2047: Security sandbox violation: parent: http://www.degoudenglimlach.be/main.swf cannot access http://www.youtube.com/[[IMPORT]]/s.ytimg.com/yt/swf/watch_as3-vflbgr4dW.swf.
我尝试在加载 swf 之前将以下内容添加到我的代码中,但没有任何区别:
I tried adding the following to my code before loading the swf but it doesn't make any difference:
Security.allowDomain("*");
Security.allowDomain("www.youtube.com");
Security.allowDomain("youtube.com");
Security.allowDomain("s.ytimg.com");
Security.allowDomain("i.ytimg.com");
任何帮助都会很棒.
这是我的完整包装类:
package be.zap.media
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.Security;
import flash.system.System;
/**
* ...
* @author Yens Resmann
*/
public class ZapYoutubeVideo extends Sprite
{
private var ytPlayer : Object;
private var ldr : Loader
private static const YOUTUBE_EMBEDDED_PLAYER_URL : String = "http://www.youtube.com/v/VIDEO_ID?version=3";
public static const PLAYER_READY : String = "playerReady";
public static const QUALITY_SMALL : String = "small";
public static const QUALITY_MEDIUM : String = "medium";
public static const QUALITY_LARGE : String = "large";
public static const QUALITY_HD720 : String = "hd720";
public static const QUALITY_HD1080 : String = "hd1080";
public static const QUALITY_HIGHRES : String = "highres";
public static const QUALITY_DEFAULT : String = "default";
public function ZapYoutubeVideo()
{
Security.allowDomain("*");
Security.allowDomain("www.youtube.com");
Security.allowDomain("youtube.com");
Security.allowDomain("s.ytimg.com");
Security.allowDomain("i.ytimg.com");
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, handleInitPlayer);
addEventListener(Event.REMOVED_FROM_STAGE, handleRemovedFromStage);
}
public function initPlayer(vidId : String)
{
var url : String = YOUTUBE_EMBEDDED_PLAYER_URL.split("VIDEO_ID").join(vidId);
ldr.load(new URLRequest(url));
}
private function handleInitPlayer(e:Event):void
{
addChild(ldr);
ldr.contentLoaderInfo.removeEventListener(Event.INIT, handleInitPlayer);
ldr.content.addEventListener("onReady", handlePlayerReady);
ldr.content.addEventListener("onError", handlePlayerError);
ldr.content.addEventListener("onStateChange", handlePlayerStageChange);
ldr.content.addEventListener("onPlaybackQualityChange", handlePlayerQualityChange);
}
private function handlePlayerReady(e:Event):void
{
ytPlayer = ldr.content;
dispatchEvent(new Event(PLAYER_READY));
}
public function queueVideoById(videoID : String, quality : String = QUALITY_DEFAULT):void
{
ytPlayer.cueVideoById(videoID, 0, quality);
}
public function loadVideoById(videoID : String, quality : String = QUALITY_DEFAULT):void
{
ytPlayer.loadVideoById(videoID, 0, quality);
}
public function queueVideoByUrl(url : String, quality : String = QUALITY_DEFAULT):void
{
ytPlayer.cueVideoByUrl(url, 0, quality);
}
public function loadVideoByUrl(url : String, quality : String = QUALITY_DEFAULT):void
{
ytPlayer.loadVideoByUrl(url, 0, quality);
}
public function setSize(w:int, h:int):void
{
ytPlayer.setSize(w, h);
}
private function handlePlayerError(e:Event):void
{
}
private function handlePlayerStageChange(e:Event):void
{
}
private function handlePlayerQualityChange(e:Event):void
{
}
private function handleRemovedFromStage(e:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE, handleRemovedFromStage);
dispose();
}
public function dispose():void
{
ytPlayer.destroy();
if (ldr) {
if (contains(ldr)) {
removeChild(ldr);
}
ldr = null;
}
}
/**
* parse out the Youtube Video ID from the video URL
* @param url
* @return String
*/
public static function getIdFromURL(url:String):String
{
var parts : Array = [];
if (url.indexOf("watch?v=") != -1) {
parts = url.split("watch?v=");
} else if (url.indexOf("watch/v/") != -1) {
parts = url.split("watch/v/");
} else if (url.indexOf("youtu.be/") != -1) {
parts = url.split("youtu.be/");
}
return String(parts[1]).split("/").join("");
}
/**
* get the thumbnail of the video
* @param String youtube Video ID
* @return URLRequest
*/
public static function getThumbnail(videoId : String):URLRequest
{
return new URLRequest("http://img.youtube.com/vi/" + videoId + "/0.jpg");
}
}
}
推荐答案
我觉得不可能,看看错误信息,你应该看到他们主要要处理 YouTube 没有更新他们的事实跨域策略文件,并且没有指定元策略.这是他们的政策文件:
I don't think it's possible , look at the error messages and you should see that they mainly have to deal with the fact that YouTube hasn't updated their crossdomain policy file and doesn't specify a meta policy. here's their policy file:
<!-- http://www.youtube.com/crossdomain.xml -->
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*.youtube.com" />
<allow-access-from domain="s.ytimg.com" />
</cross-domain-policy>
他们需要像这样添加一行:
They need to add a line like this:
<site-control permitted-cross-domain-policies="all"/>
这篇关于使用 Youtube AS3 Player API 时如何避免安全错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!