如何预加载Flash视频和图像?

时间:2012-02-24 00:34:55

标签: javascript jquery flash xhtml preloading

我正在开发一个通过ajax通过'pages'进行的webapp。每个页面的内容都在一个xml文件中,app ajax是xml文件并从中构建页面,然后将其吐出到浏览器中。

其中一些页面有视频或大图像,我试图在上一页上预加载。下面是我用来检查媒体是否已预加载的代码,但是当我登陆页面时,它似乎再次加载...任何想法?

视频播放器始终存在于dom中,当它未被使用时,我将其隐藏在屏幕外。

我假设使用新的Image()并给它一个源缓存图像太对了?

var l_image = new Image();
//other stuff happens here

switch(l_next.type) {
    case 'st_animation':
      if(l_next.video != undefined && l_next.video != '') {
        l_videoSrc = String(l_next.video);
        _videoPlayer.loadVideo(l_videoSrc);
        delete l_next;
      }
      //give 2secs for the video to load atleast the first frame
      setTimeout(p_callback, 2000);
      break;

    default:
      if(l_next.image != undefined && l_next.image != '') {
        l_imageSrc = 'files/'+ l_next.image;
        delete l_next;
        l_image.src = l_imageSrc;
        //replace the image or append it
        if(this.data.type == 'st_animation') {
          _$image.html('<img src="'+ l_imageSrc +'" alt="" />');
        }
        else {
          _$image.prepend('<img src="'+ l_imageSrc +'" alt="" />');
        }
        //trigger callback when loaded
        if(l_image.complete) {
          setTimeout(p_callback, 500);
        }
        else {
          l_image.onload = function() {
            setTimeout(p_callback, 500);
          }
        }
      }

和回调函数:

/*
 * Goes to the page with the specified id
 */
goTo : function(p_pageID) {
  //empty content & show loader
  _$content.empty();
  _currentPage = null; //empty the page data
  //_$loader.fadeIn(500);
  //get the page we're going to's data
  var l_data = this.getData(p_pageID);
  //instantiate this pages PageType sub-class
  eval('_currentPage = new '+ l_data.type +'(l_data)');
  l_data = null;
},


/**
 * Loads the xml of the page's id you pass it
 */
getData : function(p_pageID) {
  var l_cacheBuster = '?cacheBuster='+ _structure.course.settings.cache_buster,
      l_xmlPath = './data/'+ p_pageID +'.xml'+ l_cacheBuster,
      l_data = new Object();
  //ajax request
  $.ajax({
    type: 'GET',
    url: l_xmlPath,
    dataType: 'xml',
    async: false,
    success: function(p_data) {
      //convert the xml structure to json
      l_data = $.xml2json(p_data);
      //check for parsing error
      if(l_data.text != undefined) {
        var l_dataString = String(l_data);
        if(l_dataString.indexOf('XML Parsing Error') > -1) {
          trace(l_dataString);
        }
      }
    },
    error: function(p_response, p_status, p_error) {
      trace('Could not load "'+ l_xmlPath +"\"\r\n"+ p_status +': '+ p_error.name);
    }
  });
  return l_data;
}

提前致谢...

1 个答案:

答案 0 :(得分:0)

图片预加载是一个已解决的问题,因此无需reinvent - the - wheel。由于Flash控制视频,因此必须使用ActionScript完成preloading the swf

<强>参考