E
E
exaller2015-09-24 14:12:39
JavaScript
exaller, 2015-09-24 14:12:39

How to play HTML 5 video from the beginning using JS?

Is there a video:

<video loop="loop" preload="auto" class="page_header__video_item" id="video_bg" autoplay muted>
  <source media="only screen" type="video/mp4" src="/theme/edsro/video.mp4" id="mp4">
</video>

There is a code:
$('.play').click(function() {
  if ( $("video").prop('muted' )) {
    $("video").prop('muted', false);
    $(this).addClass('active');
  } else {
    $("video").prop('muted', true);
    $(this).removeClass('active');
  }
  return false;
});

What needs to be added to the code so that when the sound is turned on, the video plays first?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2015-09-24
@onqu

jsfiddle.net/edajzoqm

// получаем элемент видео
var video = document.getElementById('video');

// Флаг, по которому будем понимать, что выходим из mute
var wasMuted = video.muted;

// Время воспроизведения, на которое выставить
var unmuteTime = 0;

video.onvolumechange = function() {
    if (wasMuted) {
    	// Было в состоянии mute
    	wasMuted = false;

    	// ставим необходимое время
        video.currentTime = unmuteTime;
    } else if (video.muted) {
        // Юзер включил mute
        wasMuted = true;
    }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question