2015-02-17 77 views
0

此代碼播放Youtube視頻。該文件使用FlashBuilder中的調試或運行按鈕工作,但在從Web服務器或Export Release文件夾中播放時存在錯誤:第二個視頻運行時,第一個視頻的音頻不斷播放。我怎樣才能卸載第一個音頻?如何停止播放視頻文件中的音頻?

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> 
<mx:Script> 
    <![CDATA[ 
     import mx.controls.SWFLoader; 

     private var swfLoader:SWFLoader; 
     [Bindable] public var videoToPlay:String 
     public function playVideo(videoNum:int):void 
     { 
      if(swfLoader) 
      { 
       swfLoader.autoLoad = false; 
       swfLoader.unloadAndStop(); 
       hg.removeChild(swfLoader);    
       SoundMixer.stopAll(); 
      } 
      var videoAddress:String 
      if(videoNum == 1){ 
       videoAddress = "m2dg6teC7fg"; 
      }else{ 
       videoAddress = "0QRO3gKj3qw";    
      } 
      videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;  
      play(); 
     }    
     public function play():void 
     { 
      swfLoader = new SWFLoader(); 
      swfLoader.y = 100; 
      swfLoader.load(videoToPlay);  
      hg.addChild(swfLoader); 
     } 

    ]]> 
</mx:Script> 
<mx:HBox id="hg"> 
    <mx:Button id="button1" y="50" label="Button1" click="playVideo(1)" useHandCursor="true" buttonMode="true" /> 
    <mx:Button id="button2" y="50" label="Button2" click="playVideo(2)" useHandCursor="true" buttonMode="true" />   
</mx:HBox> 
</mx:Application> 

回答

0

的解決方案,以停止對第二個按鈕點擊第一視頻/音頻:

在代碼中使用以下從的SWFLoader卸載第一視頻:

<fx:Script> 
     <![CDATA[ 
      import mx.controls.SWFLoader; 

     private var swfLoader:SWFLoader; 
     [Bindable] public var videoToPlay:String 
     public function playVideo(videoNum:int):void 
     { 
      if(swfLoader) 
      { 
       swfLoader.autoLoad = false; 
       swfLoader.unloadAndStop(); 
//    swfLoader.autoLoad = true; 
       hg.removeElement(swfLoader);    
       SoundMixer.stopAll(); 
      } 
      var videoAddress:String 
      if(videoNum == 1){ 
      videoAddress = "m2dg6teC7fg"; 
      }else{ 
      videoAddress = "0QRO3gKj3qw";    
      } 
      videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;  
      play(); 
     }    
     public function play():void 
     { 
      swfLoader = new SWFLoader(); 
      swfLoader.y = 100; 
      swfLoader.load(videoToPlay);  
      hg.addElement(swfLoader); 
     } 

     ]]> 
    </fx:Script> 


<s:HGroup id="hg"> 
    <s:Button id="button1" y="50" label="Button1" click="playVideo(1)" useHandCursor="true" buttonMode="true" /> 
    <s:Button id="button2" y="50" label="Button2" click="playVideo(2)" useHandCursor="true" buttonMode="true" />   

</s:HGroup> 

我在下面編輯的代碼,它適用於你的問題(我測試了我的方面)。試試吧,讓我知道它的作品?

編輯:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"> 
<mx:Script> 
    <![CDATA[ 
     import mx.controls.SWFLoader; 

     private var swfLoader:SWFLoader; 
     [Bindable] public var videoToPlay:String 
     public function playVideo(videoNum:int):void 
     { 
      if(swfLoader) 
      { 
       swfLoader.source = ""; 
       swfLoader.load(null); 
       SoundMixer.stopAll(); 
      } 
      var videoAddress:String 
      if(videoNum == 1){ 
       videoAddress = "m2dg6teC7fg"; 
      }else{ 
       videoAddress = "0QRO3gKj3qw";    
      } 
      videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;  
      play(); 
     }     
     public function play():void 
     { 
      swfLoader = new SWFLoader(); 
      swfLoader.y = 100; 
      swfLoader.load(videoToPlay);  
      hg.addChild(swfLoader); 
     } 

     private function onRemove():void 
     { 

     } 
    ]]> 
</mx:Script> 
<mx:HBox id="hg"> 
    <mx:Button id="button1" y="50" label="Button1" click="playVideo(1)" useHandCursor="true" buttonMode="true" /> 
    <mx:Button id="button2" y="50" label="Button2" click="playVideo(2)" useHandCursor="true" buttonMode="true" />   
</mx:HBox> 
</mx:Application> 
+0

我加入你的代碼,以我的例子(見上面的代碼),但問題仍然存在。 – SimonRH 2015-02-25 13:08:46

+0

非常感謝 - 您的代碼在Flex4中完美工作。我的模塊是用Flex3.5編寫的一個更大的項目的一部分,但您的代碼使用Flex 3.5中不提供的addElement。有沒有辦法在3.5中做到這一點? – SimonRH 2015-02-26 13:22:13

+0

你應該使用addChild而不是addElement。 – ketan 2015-02-27 03:31:01