Capítulo 3

Vídeo - Exemplo 12, Passo 8


0%

JavaScript

<script type="text/javascript">
function init() {	 
var video = document.getElementById('video');
var slider = document.getElementById('slider');
var vol = document.getElementById('vol');	
	slider.addEventListener('change', function(){ 
		video.volume = this.value;
		vol.innerHTML = (this.value);	
	}, false);

	var btn = document.createElement('button');
	btn.innerHTML = '&#x25BA;';
	var secao = document.getElementsByTagName('section')[0];
	secao.insertBefore(btn, slider); 
	
	var btnFF = document.createElement('button');
	btnFF.innerHTML = '&#x25BA;&#x25BA;';
	secao.insertBefore(btnFF, slider); 

	var btnRW = document.createElement('button');
	btnRW.innerHTML = '&#x25C4;&#x25C4;';
	secao.insertBefore(btnRW, slider); 
	
	var btnSM = document.createElement('button');
	btnSM.innerHTML = '&#x25BA;&#x2590;';
	secao.insertBefore(btnSM, slider); 
	var video = document.getElementById('video');
	video.removeAttribute('controls');
	var playPause = function() { 
		if (video.paused || video.ended){
			video.play();
			this.innerHTML = '&#x2588; &#x2588;';
		}else{
			video.pause()
			this.innerHTML = '&#x25B6';
		}
	}
btn.addEventListener('click', playPause, false);
video.addEventListener('ended', function() {
	btn.innerHTML = '&#x25B6';
}, false);
video.addEventListener('pause', function(){
btn.innerHTML = '&#x25B6';
}, false);
video.addEventListener('play', function(){
btn.innerHTML = '&#x2588; &#x2588;';
}, false);
btnFF.addEventListener('click', function(){
	video.play();
	video.playbackRate = 3;
}, false);
btnRW.addEventListener('click', function(){
	video.play();
	video.playbackRate = -20;
}, false);
btnSM.addEventListener('click', function(){
	video.play();
	video.playbackRate = 0.3;
}, false);
var progress = document.getElementsByTagName('progress')[0];
var pp = document.getElementById('pp');
	function atualizar() {
		progress.setAttribute('max', video.duration);
		var cT = video.currentTime;
		progress.value = cT;
		var porcentagem = cT/video.duration * 100;
	if(parseInt(porcentagem%2) == 0) {
		pp.textContent = parseInt(porcentagem);
	if(porcentagem == 100) {clearInterval(atualizar)}
	}
}
setInterval(atualizar, 300);
}
window.onload = init;		
		

HTML

	<video poster="capa.jpg" controls id="video" width="400">
		<source src="video.ogv" type="video/ogg">
		<source src="video.mp4" type="video/mp4">
		<source src="video.webm" type="video/webm">
		<!-- Código (X)HTML para inserção de video com Flash -->
		<p>Seu navegador não suporta o elemento video da HTML5.<br>Faça <a href="video.mp4">download do video</a></p>
	</video>		
<input type="range" min="0" max="1" value="1" step="0.1" id="slider" />
<span id="vol">1</span>