简体   繁体   English

如何使用jQuery预加载幻灯片的多个图像,但不是全部图像

[英]how to preload more than one but not all images of a slideshow with jquery

I'd like to create a web based stop motion video player. 我想创建一个基于网络的定格视频播放器。 Basically a slideshow that shows 2-4 images per second. 基本上是每秒显示2-4张图像的幻灯片。 Each image might be a maximum of 20KB. 每个图像最大为20KB。 I don't want to preload all images in the slideshow as there might be thousands, however I need to preload more than just the next image in the show as this will not playback fast enough (because of the playback speed the browser needs to be loading more than one image at a time). 我不想预载幻灯片中的所有图像,因为可能有数千张图像,但是我需要预载节目中的下一张图像,而不仅仅是下一张图像,因为这样回放的速度不够快(由于浏览器的回放速度需要提高一次加载多个图片)。

I've been looking at using the jQuery Cycle Plugin ( http://malsup.com/jquery/cycle/ ) with a addSlide type function but don't know how to make it work. 我一直在研究将jQuery Cycle Plugin( http://malsup.com/jquery/cycle/ )与addSlide类型函数一起使用,但不知道如何使其工作。

Would something like this might work? 这样的事情可能行得通吗? -Slideshow starts -image is played back -preloader will attempt to load up to the next 60 images -playback will wait for the next image in line to completely load, but will not wait for all 59 others. -Slideshow开始-回放图像-预加载器将尝试加载最多60张图像-回放将等待行中的下一幅图像完全加载,但不会等待其他59张图像。

The playback / preloading order is important for this application. 播放/预加载顺序对于此应用程序很重要。

I don't have the exact code in a usable way, but I can explain the framework. 我没有可用的确切代码,但是我可以解释框架。 I have done something very similar in jQuery-- although I would say it's not so much jQuery as Javascript. 我在jQuery中做过非常类似的事情-尽管我说它不像Javascript那样是jQuery。 Here's the basics of what I did... 这是我所做的基础...

Create a function that "preloads" an image. 创建一个“预加载”图像的函数。 The way to do this in Javascript is simply to create an Image element, eg: 用Javascript做到这一点的方法仅仅是创建一个Image元素,例如:

  `function preloadAnImage(src) {
    var i = new Image(1020, 492);
    i.src = src;
  }`

Create a list of images, eg. 创建图像列表,例如。 imagesToPreloadIndex = ['image1.jpg','image2.jpg'... ];

Create a function that works off this queue, eg: 创建一个可以处理此队列的函数,例如:

function preloadNextImage() {
    preloadAnImage(imagesToPreload.pop());   
    if (imagesToPreloadIndex.length > 0) {
      setTimeout(preloadNextImage, 300);
    }
  }

That gives you the framework. 这样就为您提供了框架。

You'll want this synced up with the actual displaying of the images. 您需要将其与图像的实际显示同步。 This works fine as long as everything goes right. 只要一切顺利,此方法就可以正常工作。 Then you need to decide what to do if you are getting behind in your loading... do you drop a frame or slow down your slideshow? 然后,您需要确定如果在加载过程中落后了该怎么办...是放帧还是放慢幻灯片的播放速度? I'm not familiar with a plugin that implements this strategy, but would be fun to implement. 我不熟悉实现此策略的插件,但是实现起来会很有趣。

  • You can create a function that pre-loads N images , when N images are loaded it calls itself again, How much is N ? 您可以创建一个预加载N张图像的函数,当加载N张图像时,它会再次调用自身,N是多少? can be 5 or 10 or you can come up with some formula to calculate N based on expected Images dimensions/Size and time duration of displaying 可以是5或10,或者您可以根据预期的图像尺寸/大小和显示时间得出一些公式来计算N
  • If all images size are almost the same , first image requested to load should finish loading first, So it wouldn't wait for all 59 others. 如果所有图像的大小几乎相同,则请求加载的第一个图像应先完成加载,因此它不会等待所有59个其他图像。
  • plus a variable 'loadedN' that holds index of last loaded image 加上一个变量“ loadedN”,该变量保存最近加载的图像的索引
  • FireBug is certainly needed to debug this App. 当然需要FireBug来调试此应用程序。
  • Playback function needs to check if the requested image index is loaded or not 播放功能需要检查请求的图像索引是否已加载

I could imagine that this has to be multiprocessing. 我可以想象这必须是多处理的。 What I am thinking of is split the playback logic from the image preload logic. 我在想的是将回放逻辑与图像预加载逻辑分开。 It works like this... 它是这样的...

You start an initializer. 您启动一个初始化程序。 Once N pictures are preloaded (you can show the dreaded "buffering" screen at this point), you call 2 functions. 一旦预加载了N张图片(此时您可以显示可怕的“缓冲”屏幕),就可以调用2个功能。

Function 1 - shows an image, then starts a timer after which it is called again to show the next image. 功能1-显示图像,然后启动计时器,然后再次调用它以显示下一张图像。 With 4 images per second, it would call itself every 250ms. 每秒有4张图像,它每250ms就会调用一次。

Function 2 - starts a timer. 功能2-启动计时器。 Here you can work with "expected values". 在这里,您可以使用“期望值”。 You can expect that, for instance, 60 images are done processing after 15 seconds. 您可以预期,例如15秒后将处理60张图像。 So, after 7 seconds, you start loading the next 30 or 60 images and add those to the image array, all while the other function is still showing of the already present images. 因此,在7秒钟后,您将开始加载接下来的30或60张图像,并将其添加到图像阵列中,而其他功能仍将显示已经存在的图像。

Of course, you need a limiter on function 1, which stops playback at the Nth-1 element in order to avoid stack overflows or I/O Errors. 当然,您需要在功能1上使用一个限制器,该限制器会在Nth-1元素处停止播放,以避免堆栈溢出或I / O错误。

Don't know if this was useful, but it is how I understood the question. 不知道这是否有用,但这是我对问题的理解。

Sounds problematic as you'll never be able to control how fast the client can attain the images. 听起来有问题,因为您将永远无法控制客户端获得图像的速度。 To use video would probably be an easier and more satisfactory result. 使用视频可能会更简单,更令人满意。

To make this work in js you'd have to build a buffering object which can pause and resume the playback whenever the count of preloaded images falls under/over a minimal threshold; 要在js中实现此功能,您必须构建一个缓冲对象,每当预加载的图像计数低于/超过最小阈值时,该缓冲对象都可以暂停并继续播放; like flash movies. 像Flash电影一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM