1 Reply Latest reply on Jun 30, 2011 6:38 PM by wennycooper

    multiple AS3 youtube players problem

    wennycooper Level 1

      Hi all,

       

      I'm new to here.

      I tried to use youtube player API of SWF to have 4 youtube players concurrently to play videos.

      Unfortunatily, part of my 4 players just failed to work. And the result is not predicted everytime.

       

      Here is my code. Is anyone can help?? Thanks a lot!!

       

      ==================================

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                     creationComplete="init();">

       

          <fx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  import spark.components.BorderContainer;
                 
                  private var youtubeUrl0:String = "http://www.youtube.com/v/aFU-xlOvBMg?version=3";
                  private var youtubeUrl1:String = "http://www.youtube.com/v/NUjPktylfio?version=3";
                  private var youtubeUrl2:String = "http://www.youtube.com/v/lZFyurjixKo?version=3";
                  private var youtubeUrl3:String = "http://www.youtube.com/v/6QiZQYPtI7c?version=3";
                 
                 
                  private var bordercontainer0:BorderContainer = new BorderContainer();
                  private var bordercontainer1:BorderContainer = new BorderContainer();
                  private var bordercontainer2:BorderContainer = new BorderContainer();
                  private var bordercontainer3:BorderContainer = new BorderContainer();
                 
                  Security.allowDomain("www.youtube.com");
                  private var player0:Object;
                  private var player1:Object;
                  private var player2:Object;
                  private var player3:Object;
                 
                  private var loader0:SWFLoader = new SWFLoader();
                  private var loader1:SWFLoader = new SWFLoader();
                  private var loader2:SWFLoader = new SWFLoader();
                  private var loader3:SWFLoader = new SWFLoader();
                 
                 
                  private function init():void{ 
                             
                      bordercontainer0.x = 0;
                      bordercontainer0.y = 0;
                      bordercontainer1.x = 400;
                      bordercontainer1.y = 0;
                      bordercontainer2.x = 0;
                      bordercontainer2.y = 250;
                      bordercontainer3.x = 400;
                      bordercontainer3.y = 250;
                     
                      bordercontainer0.visible = true;
                      bordercontainer1.visible = true;
                      bordercontainer2.visible = true;
                      bordercontainer3.visible = true;
                     
                      bordercontainer0.addElement(loader0);
                      bordercontainer1.addElement(loader1);
                      bordercontainer2.addElement(loader2);
                      bordercontainer3.addElement(loader3);
                     
                      loader0.source = youtubeUrl0;
                      loader1.source = youtubeUrl1;
                      loader2.source = youtubeUrl2;
                      loader3.source = youtubeUrl3;
                     
                      loader0.addEventListener(Event.COMPLETE, onLoader0Init);
                      loader1.addEventListener(Event.COMPLETE, onLoader1Init);
                      loader2.addEventListener(Event.COMPLETE, onLoader2Init);
                      loader3.addEventListener(Event.COMPLETE, onLoader3Init);
                     
                      this.addElement(bordercontainer0);
                      this.addElement(bordercontainer1);
                      this.addElement(bordercontainer2);
                      this.addElement(bordercontainer3);
                     
                  }
                 
                  private function onLoader0Init(event:Event):void {
                      txtMessages.text += "onLoader0Init()\n";
                      loader0.content.addEventListener("onReady", onPlayer0Ready);
                  }
                 
                  private function onLoader1Init(event:Event):void {
                      txtMessages.text += "onLoader1Init()\n";
                      loader0.content.addEventListener("onReady", onPlayer1Ready);
                  }
                 
                  private function onLoader2Init(event:Event):void {
                      txtMessages.text += "onLoader2Init()\n";
                      loader0.content.addEventListener("onReady", onPlayer2Ready);
                  }
                 
                  private function onLoader3Init(event:Event):void {
                      txtMessages.text += "onLoader3Init()\n";
                      loader0.content.addEventListener("onReady", onPlayer3Ready);
                  }
                 
                 
                  private function onPlayer0Ready(event:Event):void {
                      txtMessages.text += "onPlayer0Ready()\n";
                      player0 = loader0.content;
                      player0.setSize(320, 240);
                      player0.playVideo();
                      //player0.unMute();
                  }
                 
                  private function onPlayer1Ready(event:Event):void {
                      txtMessages.text += "onPlayer1Ready()\n";
                      player1 = loader1.content;
                      player1.setSize(320, 240);
                      player1.playVideo();
                      player1.mute();
                  }
                 
                  private function onPlayer2Ready(event:Event):void {
                      txtMessages.text += "onPlayer2Ready()\n";
                      player2 = loader2.content;
                      player2.setSize(320, 240);
                      player2.playVideo();
                      player2.mute();
                  }
                 
                  private function onPlayer3Ready(event:Event):void {
                      txtMessages.text += "onPlayer3Ready()\n";
                      player3 = loader1.content;
                      player3.setSize(320, 240);
                      player3.playVideo();
                      player3.mute();
                  }
                 
                  protected function myButton0_clickHandler(event:MouseEvent):void
                  {
                      // TODO Auto-generated method stub
                      if (bordercontainer0.visible) {
                          bordercontainer0.visible = false;
                          bordercontainer1.visible = true;
                          player0.mute();
                          player1.unMute();
                      }
                      else    {
                          bordercontainer1.visible = false;
                          bordercontainer0.visible = true;
                          player1.mute();
                          player0.unMute();
                      }
                  }
              ]]>
          </fx:Script>

       

          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <s:Button x="175" y="569" label="Button" enabled="true" id="myButton0" click="myButton0_clickHandler(event)"/>
         
          <mx:SWFLoader/>
          <s:TextArea x="30" y="700" width="377" id="txtMessages" enabled="true"/>
         
      </s:Application>