2011-07-05 27 views
0

這裏的理念,爲這個相對簡單的Flash應用程序,我的建築:獲取圖像/視頻,顯示每過一定的時間

  1. 檢查服務器上的XML文件。
  2. 基於標籤顯示內容,無論類型是圖像還是視頻。
  3. 每個都有一個displayTime值。內容應該保持這麼長時間,然後繼續下一個。

因此,這裏是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 

<livefeed> 

    <timeUpdated string="121213" /> 

    <content type="image" url='image01.jpg' displayTime="5" /> 
    <content type="image" url='image02.jpg' displayTime="5" /> 
    <content type="image" url='image03.jpg' displayTime="5" /> 

</livefeed> 

這裏有點我的ActionScript 3的:

function onload(e:Event):void { 
    var xml:XMLList = new XMLList(xmlholder.data); 
    var xmlContent:XMLList = xml.content; 
    if([email protected] != currentTimeUpdated) { 
     currentTimeUpdated = [email protected]; 

     for each (var content:XML in xmlContent) { 
      if ([email protected] == 'image') { 
       var myImageLoader:Loader = new Loader(); 
       var imageURLRequest:URLRequest = new URLRequest([email protected]); 

       myImageLoader.load(imageURLRequest); 
       myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 

       function imageLoaded(e:Event):void {         
        var newTimer:Timer = new Timer([email protected] * 1000); // update every 10 seconds 
        newTimer.start(); 
        newTimer.addEventListener(TimerEvent.TIMER, addImage); 

        function addImage(e:TimerEvent):void { 
         addChild(myImageLoader); 
        } 

       } 
      } 
     } 
    } 
} 

所以它不是爲我工作,我希望它。我知道我正在討論這個錯誤,但不知道該怎麼做。我真的希望它只顯示內容,暫停,轉到下一個內容,暫停,然後循環。

關於我應該怎樣做或如何改善的任何想法/建議?

謝謝!

回答

0

此代碼目前有效嗎?你似乎太多地陷入了一個單一的功能 - 一次運行一堆裝載機,並使用嵌套函數來處理結果。我沒有嘗試過,但看起來有點困惑。 (我嘗試遠離嵌套函數,順便說一句)。

我會把所有這些分解成一個單獨的函數鏈。你需要的是,一旦XML已經加載順序:

  1. 啓動圖像加載
  2. 響應負載事件
  3. 然後拍攝照片作爲加載的數據,
  4. 添加圖像到顯示列表,刪除先前加載的圖像,最後,啓動一個計時器,當它啓動時,再次啓動序列。

你將需要照顧到的圖像隊列另一個XML分析器/控制器功能 - 這只是檢查當前圖像計數,遞增它,並再次指使負載序列。

您可能需要添加幾個步驟來在加載運行時運行加載微調器圖形,或者以某種方式處理從一個圖形/視頻到下一個圖形/視頻的交換。

希望你已經掌握了as3並能從這裏弄清楚這一點。讓我知道你是否有任何掛斷。我確實有完整的課程,完成所有這些,如果您需要,我可以分享。

乾杯

- 更新 -

For now, it "works" as in, the script pauses for 5 seconds (with a blank canvas), then displays all the images at once instead of sequentially

這是因爲你開始你所有的裝載機在一個單一的foreach循環,而不是隨着時間的推移激活單個序列。絕對不是你想要的。

- 隊列處理 -

所以,你有你的XML對象,這僅僅是一個重複的數據結構。重要的是,它有一個長度屬性,可以作爲索引進行導航。要以鏈式序列遍歷它,您可能需要一個可以遞增/遞減的獨立計數器變量。

private var _count:uint = 0; 

private function controlQueue(){ 

    var target: String = xmlContent[_count][email protected]; //or however you are obtaining this 
    initiateLoad(target); 

    _count++: //increment counter 

    if(_count > xmlContent.length){ _count = 0}; //if counter exceeds length, swing back around 
} 

private function initiateLoad(target:String){ ... 

如果允許用戶控制,則可以通過controlQueue a「方向」的參數,它可以規定_count ++或_count--;

非常簡單,但方便。

希望有幫助 -

+0

這波斯沃思,我很欣賞這個建議。現在,它「起作用」,腳本暫停5秒(空白畫布),然後一次顯示所有圖像,而不是按順序顯示。 – dallen

+0

你介意給我一個關於我如何做圖像隊列的例子嗎? – dallen

+0

@dallen - 查看我的更新 – Bosworth99

0

我寫了一些非常相似的東西。這是工作代碼,您可以按原樣使用它或更改它。玩的開心! * Nicholas

順便說一下,點擊切換暫停。不幸的是loadPrevious()沒有實現。只需複製並更改loadNext()方法

像這樣的東西應該可以工作。如果你想逆循環

// load Previous 
if(__currentIndex>1){ 
    __currentIndex--; 
}else{ 
    __currentIndex=__xml.child("image").length()-1; 
} 

這裏是Demo類(如何使用ImageSlideshow)

package 
{ 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import de.goldsource.display.ImageSlideshow; 

    public class Demo extends Sprite 
    { 

     public function Demo() 
     { 

      // setup 
      stage.scaleMode=StageScaleMode.NO_SCALE; 
      stage.align=StageAlign.TOP_LEFT; 
      var xml:XML = <slideshow> 
           <image src='assets/img1.jpg' delay="0.1" /> 
           <image src='assets/img2.jpg' delay="0.1" /> 
           <image src='assets/img3.jpg' delay="0.1" /> 
          </slideshow>; 
      ImageSlideshow.DEBUG = true; 
      addChild(new ImageSlideshow(xml));   
     } 
    } 
} 

的幻燈片類。您可以使用它並更改它,但保留許可證文本。該軟件包是de.goldsource.display將此代碼保存在一個名爲ImageSlideshow.as的文件中,並將其放入de/goldsource/display /相對於您的src root或lib目錄中。

package de.goldsource.display 
{ 
    import flash.display.Bitmap; 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.events.IOErrorEvent; 
    import flash.events.MouseEvent; 
    import flash.events.TimerEvent; 
    import flash.net.URLRequest; 
    import flash.system.System; 
    import flash.utils.Timer; 
    import flash.utils.setTimeout; 

    /** 
    * 
    * <p> 
    * <b>License</b> 
    * <br/> 
    * <a href="http://www.opensource.org/licenses/mit-license.php">based on The MIT License (MIT)</a> 
    * </p> 
    * <p> 
    * Copyright (c) 2011 Nicholas Schreiber 
    * </p> 
    * <p> 
    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
    * </p> 
    * <p> 
    * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
    * </p> 
    * <p> 
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    * </p> 
    * 
    * @author Nicholas Schreiber 
    * */ 
    public class ImageSlideshow extends Sprite 
    { 

     /** 
     * Traces Memory Usage Info 
     * */ 
     public static var DEBUG:Boolean = false; 

     /** 
     * Slideshow data 
     */ 
     private var __xml:XML; 

     /** 
     * Main Timer 
     * */ 
     private var __timer:Timer = new Timer(1000); 

     /** 
     * True if paused 
     * */ 
     private var __paused:Boolean = false; 

     /** 
     * Current Element 
     * */ 
     private var __currentIndex:int=-1; 


     /** 
     * loads the images 
     * */ 
     private var __loader:Loader; 

     /** 
     * True if Loader has done its job 
     * */ 
     private var __loaderReady:Boolean=true; 

     /** 
     * Error Count 
     * */ 
     private var __errorCount:uint = 0; 

     /** 
     * Only for Debug Purposes 
     * */ 
     private var __lastMemoryMax:uint = 0; 

     /** 
     * ImageSlideshow - Loads and displays Images 
     * 
     * <p> 
     * jpg, gif or png possible<br/> 
     * delay in seconds 
     * </p> 
     * <p> 
     * <code> 
     * &lt;slideshow&gt;<br/> 
     * &lt;image src='assets/img1.jpg' delay="0.1" /&gt;<br/> 
     * &lt;image src='assets/img2.png' delay="2" /&gt;<br/> 
     * ...<br/> 
     * &lt;image src='assets/imgN.gif' delay="0.1" /&gt;<br/> 
     * &lt;/slideshow&gt;<br/> 
     * </code> 
     * </p> 
     * @param $xml some XML as described above 
     * */ 
     public function ImageSlideshow($xml:XML=null) 
     { 
      __xml = $xml;  
      __loader = new Loader(); 

      // Click toggles Pause 
      addEventListener(MouseEvent.CLICK,__onClick); 

      // The Update Interval 
      __timer.addEventListener(TimerEvent.TIMER,__onTimer); 

      // load the first image 
      loadNext(); 
     } 

     /** 
     * Toggle Pause 
     * */ 
     public function togglePause():void{ 
      if(__paused){ 
       __paused = false; 
      }else{        
       __paused = true; 
      } 
      __updateTimerStatus(); 
     } 

     /** 
     * loadNext Image (looped) 
     * */ 
     public function loadNext():void{    
      // return if not possible 
      if(!__loaderReady || __xml == null)return; 

      // loader is blocked 
      __loaderReady = false; 

      // load Next 
      if(__currentIndex<__xml.child("image").length()-1){ 
       // slides availabe 
       __currentIndex++; 
      }else{ 
       // no more slides, looping the slideshow... 
       __currentIndex=0; 
      } 

      // resetting timer 
      __timer.stop(); 
      __timer.reset(); 
      // setting delay 
      __timer.delay = __xml.child("image")[__currentIndex][email protected]*1000; 

      // setup loader 
      __loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __onLoadComplete); 
      __loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,__onIOError); 

      // load 
      __loader.load(new URLRequest(__xml.child("image")[__currentIndex][email protected]));    
     } 

     /** 
     * toggles Pause on Click 
     * */  
     private function __onClick($e:MouseEvent):void{ 
      togglePause(); 
     } 

     /** 
     * starts the timer if possible or stops it if paused 
     * */ 
     private function __updateTimerStatus():void{  
      if(!__loaderReady)return; 
      if(__paused && __timer.running){ 
       __timer.stop(); 
      }else{        
       __timer.start(); 
      } 

     } 

     /** 
     * Invoked by the timer 
     * */ 
     private function __onTimer(e:TimerEvent):void{ 
      loadNext(); 
     }  

     /** 
     * Invoked if image is not existent (wrong src url!) 
     * */ 
     private function __onIOError(e:IOErrorEvent):void{   
      // remove listeners 
      __loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onLoadComplete); 
      __loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,__onIOError);      

      // iterate error count, will stop the app when > 10 in a row 
      __errorCount ++;    
      trace("Image could not be loaded "+__errorCount +" of 10 attempts"); 


      if(__errorCount > 10)return; 

      // if more than one image in list try next 
      __loaderReady = true; 
      if(__xml.child("image").length()>1)loadNext();   
     } 

     /** 
     * Invoked when image is loaded 
     * */ 
     private function __onLoadComplete(e:Event):void{  
      // remove the listeners 
      __loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onLoadComplete); 
      __loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,__onIOError); 

      // reset the error count, cause on image was loaded successful 
      __errorCount = 0; 

      // add the loaded image to the display 
      addChild(__loader.content); 

      // yes the loader is no longer blocked 
      __loaderReady = true; 

      // restarts the timer if was running 
      __updateTimerStatus(); 

      // removes 
      __cleanUp(); 
     } 

     /** 
     * Removes all unnecessary Bitmaps 
     * */ 
     private function __cleanUp():void{ 
      // leave method if nothing to do (only one object on screen) 
      if(numChildren==1)return; 

      /* 
       never forget to dispose() a bitmap you want to get rid of, 
       otherwise FlashPlayer will not remove it from the RAM 
       removing and disposing within one line 
      */ 
      if(getChildAt(0) is Bitmap)Bitmap(removeChildAt(0)).bitmapData.dispose(); 

      // Debug mode only 
      if(!DEBUG)return; 
      if(__lastMemoryMax<System.totalMemory){ 
       trace("New Memory Peak "+((__lastMemoryMax= System.totalMemory)/Math.pow(1024,2)).toFixed(2)+" MB"); 
      }   
     } 

     /** 
     * xml Getter/Setter - Slideshow data 
     * <p> 
     * jpg, gif or png possible<br/> 
     * delay in seconds 
     * </p> 
     * <p> 
     * <code> 
     * &lt;slideshow&gt;<br/> 
     * &lt;image src='assets/img1.jpg' delay="0.1" /&gt;<br/> 
     * &lt;image src='assets/img2.png' delay="2" /&gt;<br/> 
     * ...<br/> 
     * &lt;image src='assets/imgN.gif' delay="0.1" /&gt;<br/> 
     * &lt;/slideshow&gt;<br/> 
     * </code> 
     * </p> 
     * 
     * @param value XML 
     * @return XML 
     * */ 
     public function get xml():XML 
     { 
      return __xml; 
     } 

     public function set xml(value:XML):void 
     { 
      __xml = value; 
      __currentIndex = -1; 
      loadNext(); 
     } 


    } 
} 
+0

看起來不錯,謝謝!我會給它一個鏡頭。我可能遇到的唯一問題是,我的XML中的一些項目將是視頻,而不僅僅是圖像。我會玩弄它。 – dallen

+0

我正在使用您的腳本,但出現以下錯誤。顯示第一個圖像,然後在延遲時間之後,這是錯誤:http://i.imgur.com/sfiCy.png – dallen

+0

以下是有問題的行:http://i.imgur.com/ ItgOG.png – dallen