M.Stark M.Stark - 5 months ago 13
Javascript Question

HTML5 Video pause with Javascript when starting to play another video

I was just wondering is there any option to create a Javascript that pauses a video that is currently playing when You click play on another video.
I've got about 30-40 HTML5 video tags on one page.

Code that I'm using to put videos on page:

function pobierz_klipy($typ) {
$result = mysql_query("SELECT * FROM Klipy WHERE typ = $typ ORDER BY id")
or die("Zapytanie niepoprawne");
//ilosc zwroconych wierszy
$num_rows = mysql_num_rows($result);
$i = 0;
while ($i < $num_rows) {
$id = mysql_result($result, $i, "id");
$nazwa = mysql_result($result, $i, "nazwa");
$nazwa_rob = mysql_result($result, $i, "nazwa_rob");
$typ = mysql_result($result, $i, "typ");
$sprawdzian_hq = $sciezka = "../video/data/video/" . $nazwa_rob . "_fb.mp4";

if (file_exists($sprawdzian_hq)) {
$sciezka = "../video/data/video/" . $nazwa_rob . "_fb.mp4";
} else {
$sciezka = "../video/data/video/" . $nazwa_rob . ".mp4";
}
if (file_exists($sprawdzian_hq)) {
$poster = "../video/data/thumbnails/" . $nazwa_rob . "_fb.mp4.jpg";
} else {
$poster = "../video/data/thumbnails/" . $nazwa_rob . ".mp4.jpg";
}


if (file_exists($sprawdzian_hq)) {
$hq = "../video/data/video/" . $nazwa_rob . ".mp4";
} else {
$hq = null;
}
$i = $i + 1;
echo "<div class=\"klip\">";
echo '<video id="' . $nazwa_rob . '" width="320" height="180" controls poster= "' . $poster . '" preload ="none">';
echo ' <source id="source_' . $nazwa_rob . '" src="' . $sciezka . '" type="video/mp4">';
echo 'Twoja przeglądarka nie obsługuje HTML5.';
echo '</video>';
echo '<br />';
if ($hq != null) {
echo '<button id="videolink' . $i . '" type="button" class="btn btn-danger active" style=" font-size: 10px; padding: 5px; position: absolute; bottom: 2px; left: 11px;">HQ</button>';
}
echo '<div class="nazwa">'.$nazwa.'</div>';
//skrypt zmiany jakosci JS
if (file_exists($sprawdzian_hq)) {
echo '
<script type = "text/javascript">
var videobutton = document.getElementById("videolink' . $i . '");

videobutton.addEventListener("click", function (event) {
';
echo "
var videocontainer$i = document.getElementById('$nazwa_rob');
var videosource$i = document.getElementById('source_$nazwa_rob');
var newmp4$i = '$hq';
var old_mp4$i = '$sciezka';
var aktywne$i = 'btn btn-success active';
var nieaktywne$i = 'btn btn-danger active';
var przycikstatus$i = document.getElementById('videolink$i');
var obecny_klip$i = videosource$i.getAttribute('src');
var czas_klipu$i = videocontainer$i.currentTime;
console.log(czas_klipu$i);

if (obecny_klip$i != newmp4$i) {
var videosource$i = document.getElementById('source_$nazwa_rob');
videocontainer$i.pause();
videosource$i.setAttribute('src', newmp4$i);
videocontainer$i.load();
videocontainer$i.pause();
videocontainer$i.currentTime = czas_klipu$i;
przycikstatus$i.setAttribute('class', aktywne$i);
videocontainer$i.play();

} else {
var videosource$i = document . getElementById('source_$nazwa_rob');
videocontainer$i.pause();
videosource$i.setAttribute('src',old_mp4$i);
videocontainer$i.load();
videocontainer$i.pause();
videocontainer$i.currentTime = czas_klipu$i;
przycikstatus$i.setAttribute('class', nieaktywne$i);
videocontainer$i.play();

}


}, false);

</script>
";
}

Answer

you can also use this javascript code.

//external.js
   video = document.getElementsByClassName("video");
        for (i = 0; i < video.length; i++) {
            video[i].onplay = function () {
                var currentIndex = index("video", this);
                for (k = 0; k < video.length; k++) {
                    if (k == currenIndex) { continue }
                    video[k].pause();
                }
            }
        }

       
        function index(className, id) {
            nodes = document.getElementsByClassName(className);
            return [].indexOf.call(nodes, id);
        }
<html>
<head>
</head>
<body>
<video class="video" src="video1.mp4" controls></video>
    <video class="video" src="video2.mp4" controls></video>
    <video class="video" src="video3.mp4" controls></video>
    <video class="video" src="video4.mp4" controls></video>
  <script src="external.js"></script>
</body>
</html>