Dhyaneshwar Shukla.
Dhyaneshwar Shukla.

Reputation: 65

Stop the Video When Play Another Video

In my page, I have many videos and I want to stop the video when playing another video in HTML5.

<?php

$result = "SELECT data from events_gallery where `events_id`='$id'";
$query = mysqli_query($db, $result);
$rows = mysqli_num_rows($query);

if($rows > 0){
while ($res = mysqli_fetch_array($query)) {
    $image = $res['data'];
}
?>
<div class="item active">
    <video controls="controls" playsinline width="180" height="300">
        <source src="images/video.mp4" type="video/mp4">
        <p>Video is not Supporting</p>
    </video>
</div>
<?php
}
?>

Upvotes: 1

Views: 3353

Answers (2)

Mario V&#225;zquez
Mario V&#225;zquez

Reputation: 777

You should rely on HTML5 video API to control your videos. Here is a good starting point: https://www.html5rocks.com/en/tutorials/video/basics

I can't see in your code how you load all your videos, but a first try could be to stop all videos before a video is being played:

$(document).ready(function(){
    // Add an eventlistener to all videos to detect when they start playing.
    $("video").each(function(){
        var video = $(this)[0];
        video.addEventListener("playing", function() {
            // Stop all other videos.
            $("video").each(function(){
                if($(this)[0] != video)
                    video.pause(); 
            });
        }, true);
     );
  }
});

Upvotes: 0

anees
anees

Reputation: 1855

here is the way

<div class="item active"> 
    <video class="inlineVideo" controls="controls" playsinline width="180" height="300">
        <source src="images/video.mp4" type="video/mp4" >
        <p>Video is not Supporting</p>
    </video>
</div>


<script type="text/javascript">
    window.addEventListener('load', function(event){
        document.querySelectorAll(".inlineVideo").forEach((el) => {
            el.onplay = function(e){
                // pause all the videos except the current.
                document.querySelectorAll(".inlineVideo").forEach((el1) => {
                    if(el === el1)
                        el1.play();
                    else
                        el1.pause();
                });
            }
        });
    });
</script>

Upvotes: 3

Related Questions