如何使用YouTube AS3播放器API时,为避免安全错误播放器、如何使用、为避免、错误

2023-09-09 21:39:56 作者:一路荒凉如歌

我使用YouTube AS3播放器API来加载视频在Flash项目。 我正在载入播放器SWF的时候得到这个真的很烦错误:

 引发SecurityError:错误#2047:安全沙箱冲突:父:http://www.degoudenglimlach.be/main.swf无法访问http://www.youtube.com/ [ [进口] / s.ytimg.com/yt/swf/watch_as3-vflbgr4dW.swf。
 

我尝试加载的SWF之前添加以下到我的code,但它并没有任何区别:

 的Security.allowDomain(*​​);
的Security.allowDomain(www.youtube.com);
的Security.allowDomain(youtube.com);
的Security.allowDomain(s.ytimg.com);
的Security.allowDomain(i.ytimg.com);
 

任何帮助将是巨大的。

使用安卓Youtube API播放youtube视频

下面是我的完整的包装类:

 包be.zap.media
{
    进口flash.display.Loader;
    进口flash.display.Sprite;
    进口对象类型:flash.events.Event;
    进口flash.net.URLRequest;
    进口类flash.system.Security;
    进口flash.system.System;

    / **
     * ...
     * @author日元Resmann
     * /
    公共类ZapYoutubeVideo扩展Sprite
    {
        私人VAR ytPlayer:对象;
        私人VAR LDR:装载机
        私有静态常量YOUTUBE_EMBEDDED_PLAYER_URL:字符串=htt​​p://www.youtube.com/v/VIDEO_ID?version=3;

        公共静态常量PLAYER_READY:字符串=playerReady;

        公共静态常量QUALITY_SMALL:字符串=小;
        公共静态常量QUALITY_MEDIUM:字符串=中等;
        公共静态常量QUALITY_LARGE:字符串=大;
        公共静态常量QUALITY_HD720:字符串=HD720;
        公共静态常量QUALITY_HD1080:字符串=HD1080;
        公共静态常量QUALITY_HIGHRES:字符串=HIGHRES;
        公共静态常量QUALITY_DEFAULT:字符串=默认;

        公共职能ZapYoutubeVideo()
        {
            的Security.allowDomain(*​​);
            的Security.allowDomain(www.youtube.com);
            的Security.allowDomain(youtube.com);
            的Security.allowDomain(s.ytimg.com);
            的Security.allowDomain(i.ytimg.com);

            LDR =新的Loader();
            ldr.contentLoaderInfo.addEventListener(Event.INIT,handleInitPlayer);
            的addEventListener(Event.REMOVED_FROM_STAGE,handleRemovedFromStage);
        }

        公共职能initPlayer(vidId:字符串)
        {
            变种网址:字符串= YOUTUBE_EMBEDDED_PLAYER_URL.split(VIDEO_ID)加入(vidId)。
            ldr.load(新的URLRequest(URL));
        }

        私有函数handleInitPlayer(五:事件):无效
        {
            的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);
        }

        私有函数handlePlayerReady(五:事件):无效
        {
            ytPlayer = ldr.content;
            则dispatchEvent(新的事件(PLAYER_READY));
        }

        公共职能queueVideoById(VIDEOID:字符串,质量:字符串= QUALITY_DEFAULT):无效
        {
            ytPlayer.cueVideoById(VIDEOID,0,质量);
        }

        公共职能loadVideoById(VIDEOID:字符串,质量:字符串= QUALITY_DEFAULT):无效
        {
            ytPlayer.loadVideoById(VIDEOID,0,质量);
        }

        公共职能queueVideoByUrl(网址:字符串,质量:字符串= QUALITY_DEFAULT):无效
        {
            ytPlayer.cueVideoByUrl(URL,0,质量);
        }

        公共职能loadVideoByUrl(网址:字符串,质量:字符串= QUALITY_DEFAULT):无效
        {
            ytPlayer.loadVideoByUrl(URL,0,质量);
        }

        公共职能的setSize(W:INT,H:INT):无效
        {
            ytPlayer.setSize(W,H);
        }

        私有函数handlePlayerError(五:事件):无效
        {

        }

        私有函数handlePlayerStageChange(五:事件):无效
        {

        }

        私有函数handlePlayerQualityChange(五:事件):无效
        {

        }

        私有函数handleRemovedFromStage(五:事件):无效
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,handleRemovedFromStage);
            配置();
        }

        公共职能的Dispose():无效
        {
            ytPlayer.destroy();
            如果(LDR){
                如果(包含(LDR)){
                    removeChild之(LDR);
                }
                LDR = NULL;
            }
        }

        / **
         *解析出YouTube视频ID的视频网址
         * @参数的URL
         * @返回字符串
         * /
        公共静态函数getIdFromURL(网址:字符串):字符串
        {
            VAR部分:数组= [];
            如果(url.indexOf(手表?ν=)!= -1){
                部分= url.split(看V =?);
            }否则,如果(url.indexOf(手表/ V /)!= -1){
                部分= url.split(手表/ V /);
            }否则如果(url.indexOf(youtu.be/)!=  -  1){
                部分= url.split(youtu.be/);
            }
            返回字符串(地区[1])分裂(/)参加()。;
        }

        / **
         *获得视频的缩略图
         * @参数字符串的YouTube视频ID
         * @返回的URLRequest
         * /
        公共静态函数getThumbnail(VIDEOID:字符串)的URLRequest
        {
            返回新的URLRequest(http://img.youtube.com/vi/+ VIDEOID +/0.jpg);
        }

    }

}
 

解决方案

我不认为这是可能的,看的错误信息,你应该看到,他们主要是不得不面对的事实是,YouTube已经没有更新他们的跨域策略文件并没有指定元策略。这里是他们的策略文件:

 <! -  http://www.youtube.com/crossdomain.xml  - >
 <!DOCTYPE跨域政策
  SYSTEMhttp://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
 <交域政策>
  <* youtube.com允许访问,从域= />
  <允许存取来自域=s.ytimg.com/>
 < /跨域策略>
 

他们需要增加这样一行:

 <现场控制允许交叉域策略=所有/>
 

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.

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");

Any Help would be great.

Here's my full Wrapper class:

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"); 
        }

    }

}

解决方案

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"/>