“autoplay”属性会播放Django循环中的所有视频。

huangapple go评论52阅读模式
英文:

autoplay attribute plays all videos that are in django for loop

问题

I'm building a web application with Django that includes a list of video generated by a for loop. Each video element has an HTML5 video tag with a src attribute pointing to a different video file, and an autoplay attribute set to true. However, when I render the page, all videos start playing at once, overlapping each other and creating a chaotic sound.

What could be causing this behavior, and how can I fix it? Is there a way to make each video start playing only when it's visible on the screen, or when the user interacts with it? Any help would be appreciated.

I want to set the videos in my for loop to autoplay so that when a user visits the website, there is no need to click on the play and pause icons when they want to watch different videos. What I want is for the video to stop when the user scrolls down and the next one to start playing like facebook videos and other social media platform, my friend suggested that, I can use JavaScript to fix that, but how can I use JavaScript to do that?

英文:

I'm building a web application with Django that includes a list of video generated by a for loop. Each video element has an HTML5 video tag with a src attribute pointing to a different video file, and an autoplay attribute set to true. However, when I render the page, all videos start playing at once, overlapping each other and creating a chaotic sound.

What could be causing this behavior, and how can I fix it? Is there a way to make each video start playing only when it's visible on the screen, or when the user interacts with it? Any help would be appreciated.

I want to set the videos in my for loop to autoplay so that when a user visits the website, there is no need to click on the play and pause icons when they want to watch different videos. What I want is for the video to stop when the user scrolls down and the next one to start playing like facebook videos and other social media platform, my friend suggested that, I can use JavaScript to fix that, but how can I use JavaScript to do that?

<div  class="container video-container">
        <div class="row justify-content-center">

          <div class="col-md-3 p-1">
            {% for video in my_videos reversed %}
            <div class="row position-relative my-1 wrapper">
              <video loop id="my-video" class="video-js vjs-theme-city vjs-default-skin vjs-big-play-button text-center" autoplay auto="true" width="1000" controls data-setup="{}">
                <source src="{{ video.video.url }}" type="video/mp4">
              </video>
              <div class="row position-absolute bottom-0 d-flex align-items-end mb-3 mx-auto">
                <div class="col-md-10 col img_wrapper">
                  <a style="text-decoration: none;" href="{{ video.get_user_public_url }}">
                    <div class="row">
                      <div class="col-auto pe-1">
                        <img class="rounded-circle thumb" src="{{video.get_user_photo}}" alt="">
                      </div>
                      <div class="col ps-1">
                        <b class="text-white">
                            {{video.user}}
                        </b>
                        <br>
                        <small class="text-white">{{video.title}}</small>
                      </div>
                    </div>
                  </a>
                </div>
                <div class="col-md-2 col-auto video-icons d-flex flex-column">
                  <a href="">
                    <span class="material-symbols-outlined">
                        thumb_up
                    </span>
                  </a>
                  <a href="{% url 'Play-Video' video.slug %}">
                    <span class="material-symbols-outlined">
                      comment
                    </span>
                  </a>
                  <a href="{% url 'Complain-Video' video.pk %}">
                    <span class="material-symbols-outlined">
                      flag
                    </span>
                  </a>
                </div>
              </div>
            </div>
            {% endfor %}
          </div>

          
        </div>
      </div>

my css:

.thumb {
      width: 30px;
      height: auto;
    }
    .rounded-circle {
        width: 25px;
        height: 25px;
    }
    .video-icons a span{
        margin-bottom: 50px;
        color: white;
        font-size: 30px;
        
    }
    video{
        object-fit: fill;
        height: 1000px;
        width: 100%;
    }

my views:

def videos(request):
 
    my_videos = Video.objects.all()
    context = {
        'my_videos':my_videos,
    }
    return render(request, 'video/videos.html', context)

答案1

得分: 0

以下是代码的翻译部分:

"我看到的行为是因为在页面加载时设置了autoplay属性为true,导致所有视频同时开始播放。修复此问题的一种方法是添加一个脚本,如下所示:

// 获取所有视频元素
const videos = document.querySelectorAll('video');

// 当视频在视图中时播放
const playVideoInView = () => {
    for (let i = 0; i < videos.length; i++) {
        const video = videos[i];
        const rect = video.getBoundingClientRect();
        const isInView = (rect.top >= 0 && rect.bottom <= window.innerHeight);
        if (isInView) {
            video.play();
        } else {
            video.pause();
        }
    }
};

// 为滚动、调整大小和加载事件添加窗口对象的事件监听器,这将在用户滚动、调整窗口大小或加载页面时触发playVideoInView()函数。
window.addEventListener('scroll', playVideoInView);
window.addEventListener('resize', playVideoInView);
window.addEventListener('load', playVideoInView);

在这段代码中,我使用querySelectorAll()选择所有视频元素。然后,我定义了一个playVideoInView()函数,该函数使用getBoundingClientRect()检查每个视频元素是否在视图中。如果视频元素在视图中,我们使用play()方法开始播放它,否则使用pause()方法暂停它。最后,我为窗口对象添加了滚动、调整大小和加载事件的事件监听器,这些事件会在用户滚动、调整窗口大小或加载页面时触发playVideoInView()函数。"

英文:

The behavior I'm seeing is due to all videos starting to play simultaneously when the page loads because I have set the autoplay attribute to true for each video. One way to fix this issue is to add a script, so in this case:

&lt;script&gt;
    // Get all video elements
    const videos = document.querySelectorAll(&#39;video&#39;);

    // Play video when it is in view
    const playVideoInView = () =&gt; {
        for (let i = 0; i &lt; videos.length; i++) {
            const video = videos[i];
            const rect = video.getBoundingClientRect();
            const isInView = (rect.top &gt;= 0 &amp;&amp; rect.bottom &lt;= window.innerHeight);
            if (isInView) {
                video.play();
            } else {
                video.pause();
            }
        }
    };

    // Add event listener to window for scrolling and playing videos in view
    window.addEventListener(&#39;scroll&#39;, playVideoInView);
    window.addEventListener(&#39;resize&#39;, playVideoInView);
    window.addEventListener(&#39;load&#39;, playVideoInView);
&lt;/script&gt;

In this code, I select all video elements using querySelectorAll(). Then, I define a function playVideoInView() that checks if each video element is in view using getBoundingClientRect(). If a video element is in view, we start playing it using the play() method, otherwise, I pause it using the pause() method.

Finally, I add event listeners to the window object for the scroll, resize, and load events, which will trigger the playVideoInView() function whenever the user scrolls, resizes the window, or loads the page.

huangapple
  • 本文由 发表于 2023年5月7日 04:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定