我想在全屏幕上开发基于
HTML 5的幻灯片与图像和视频.当几分钟内没有用户活动时,这将是我们的一个信息亭上的某种屏幕保护程序.我们已经在全屏幕上实现了基于图像的幻灯片,所以没有问题,但现在我们还要添加视频自动播放功能,例如让我们说这是屏幕内容的顺序
> image.jpeg
> image2.jpeg
> videoclip.mp4
> image3.jpeg
> someothervide.mp4
在jquery脚本运行之后,我们想连续运行这些文件,并且在播放视频完成时显示图像几秒钟或自动启动视频并移动到下一张幻灯片
解决方法
其实这很容易解决.查找
JavaScript评论内的所有解释.将所有这些都封装在$(document).ready(function(){})中;你准备好了.
HTML
<div id="canvas" class="canvas"></div>
CSS
div.canvas { display: table-cell; width: 1280px; height: 800px; background: black; vertical-align: middle; } div.canvas > video { display: block; margin: auto; } div.canvas > img { display: block; margin: auto; }
JavaScript – 变量
// array containing links to the content var content = ['movie_1.m4v','movie_2.m4v','image_1.jpg','image_2.jpg']; // element where anything will be played var canvas = $('#canvas'); // duration an image is shown in ms (milliseconds) var durationImage = 1000; // basic source for image and video tag var srcImage = '<img src="$" alt="">'; var srcVideo = '<video autoplay><source src="$" type="video/mp4"></source></video>'; // current position var current = -1; // regex for getting file extension (from https://stackoverflow.com/questions/680929/how-to-extract-extension-from-filename-string-in-javascript) var regex = /(?:\.([^.]+))?$/;
JavaScript – 功能
// method to play the next content element function playNext() { // increase current element and set it to 0 if end is reached ++current; if (content.length == current) { current = 0; } // get file and its extension and check whether it's valid var source = null; var file = content[current]; var extension = regex.exec(file)[1]; if ('jpg' == extension) { source = srcImage; } if ('m4v' == extension) { source = srcVideo; } // if source seems valid if (null !== source) { // replace placeholder with the content and insert content into canvas source = source.replace('$',file); canvas.html(source); // if content is an image play next after set duration if ('jpg' == extension) { setTimeout(function() { playNext(); },durationImage); } // if content is a video,bind 'onend' event handler to it,to play next if ('m4v' == extension) { canvas.find('video').bind("ended",function() { playNext(); }); } } }
// show first (remember current = -1 from above :) ) playNext();
演示
演示说明:由于提供的视频格式(视频/快速时间),该演示仅在Safari中运行(也可能在IE9中运行).