简体   繁体   English

遍历 Videojs 中的视频

[英]Iterate through video's in Videojs

Thanks for looking at my question.谢谢你看我的问题。 For a assignment I need to iterate through 5 video's in a JQuery accordion.对于作业,我需要在 JQuery 手风琴中遍历 5 个视频。 We need to create our own buttons, which have their own functions.我们需要创建自己的按钮,它们有自己的功能。 At the moment I have 5 div's with different videos, however, when I want to play for instance the second video, the first video will start playing.目前我有 5 个不同视频的 div,但是,当我想播放第二个视频时,第一个视频将开始播放。 This will also be the case for the 3th, 4th and 5th video.第 3、4 和 5 个视频也是如此。

Question: How can I change my code, so each play button will be working for each different video?问题:如何更改我的代码,以便每个播放按钮适用于每个不同的视频? My guess is the JQuery Each function, but I have no clue.我的猜测是 JQuery 每个 function,但我不知道。

        <div id="video-wrap">
            <div id="accordion">
                <div class="acc-wrap"><img class="arrow" src="img/arrow.svg" alt="arrow">
                    <h3>Introduction</h3>
                </div>
                <div>
                    <div class="video-player" id="player-1">
                        <video id="my-video-1" class="video-js" preload="auto" width="1088px" height="612"
                            data-setup="{}">
                            <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4" />
                            <p class="vjs-no-js">
                                To view this video please enable JavaScript, and consider upgrading to a
                                web browser that
                                <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5
                                    video</a>
                            </p>
                        </video>
                        <div class="player-buttons">
                            <a class="play-toggle" class="video-play">
                                <img class="pause" src="img/play-btn.svg" alt="pause button">
                                <img class="play" src="img/play-btn2.svg" alt="play button">
                            </a>
                            <a class="forward-btn">
                                <img src="img/forward btn.svg" alt="forward button">
                            </a>
                            <a class="rewind-btn">
                                <img src="img/rewind btn.svg" alt="forward button">
                            </a>
                        </div>
                    </div>
                </div>
                <div class="acc-wrap"><img class="arrow" src="img/arrow.svg" alt="arrow">
                    <h3>Daniel Shiffman: Coding Challenge 1</h3>
                </div>
                <div>
                    <div class="video-player" id="player-2">
                        <video id="my-video-2" class="video-js" preload="auto" width="1088px" height="612"
                            data-setup="{}">
                            <source src="video/citral.mp4" type="video/mp4" />
                            <p class="vjs-no-js">
                                To view this video please enable JavaScript, and consider upgrading to a
                                web browser that
                                <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5
                                    video</a>
                            </p>
                        </video>
                        <div class="player-buttons">
                            <a class="play-toggle" class="video-play">
                                <img class="pause" src="img/play-btn.svg" alt="pause button">
                                <img class="play" src="img/play-btn2.svg" alt="play button">
                            </a>
                            <a class="forward-btn">
                                <img src="img/forward btn.svg" alt="forward button">
                            </a>
                            <a class="rewind-btn">
                                <img src="img/rewind btn.svg" alt="forward button">
                            </a>
                        </div>
                    </div>
                </div>
                <div class="acc-wrap"><img src="img/arrow.svg" alt="arrow">
                    <h3>Daniel Shiffman: Coding Challenge 2</h3>
                </div>
                <div>
                    <div class="video-player" id="player-3">
                        <video controls id="my-video-3" class="video-js" preload="auto" width="1088px" height="612"
                            data-setup="{}">
                            <source src="https://player.vimeo.com/video/420225730" type="video/mp4" />
                            <p class="vjs-no-js">
                                To view this video please enable JavaScript, and consider upgrading to a
                                web browser that
                                <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5
                                    video</a>
                            </p>
                        </video>
                        <div class="player-buttons">
                            <a class="play-toggle" class="video-play">
                                <img class="pause" src="img/play-btn.svg" alt="pause button">
                                <img class="play" src="img/play-btn2.svg" alt="play button">
                            </a>
                            <a class="forward-btn">
                                <img src="img/forward btn.svg" alt="forward button">
                            </a>
                            <a class="rewind-btn">
                                <img src="img/rewind btn.svg" alt="forward button">
                            </a>
                        </div>
                    </div>
                </div>
                <div class="acc-wrap"><img src="img/arrow.svg" alt="arrow">
                    <h3>Daniel Shiffman: Coding Challenge 3</h3>
                </div>
                <div>
                    <div class="video-player" id="player-4">
                        <video controls id="my-video-4" class="video-js" preload="auto" width="1088px" height="612"
                            data-setup="{}">
                            <source src="https://player.vimeo.com/video/420225730" type="video/mp4" />
                            <p class="vjs-no-js">
                                To view this video please enable JavaScript, and consider upgrading to a
                                web browser that
                                <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5
                                    video</a>
                            </p>
                        </video>
                        <div class="player-buttons">
                            <a class="play-toggle" class="video-play">
                                <img class="pause" src="img/play-btn.svg" alt="pause button">
                                <img class="play" src="img/play-btn2.svg" alt="play button">
                            </a>
                            <a class="forward-btn">
                                <img src="img/forward btn.svg" alt="forward button">
                            </a>
                            <a class="rewind-btn">
                                <img src="img/rewind btn.svg" alt="forward button">
                            </a>
                        </div>
                    </div>
                </div>
                <div class="acc-wrap"><img src="img/arrow.svg" alt="arrow">
                    <h3>Daniel Shiffman: Coding Challenge 4</h3>
                </div>
                <div>
                    <div class="video-player" id="player-5">
                        <video controls id="my-video-5" class="video-js" preload="auto" width="1088px" height="612"
                            data-setup="{}">
                            <source src="https://player.vimeo.com/video/420225730" type="video/mp4" />
                            <p class="vjs-no-js">
                                To view this video please enable JavaScript, and consider upgrading to a
                                web browser that
                                <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5
                                    video</a>
                            </p>
                        </video>
                        <div class="player-buttons">
                            <a class="play-toggle" class="video-play">
                                <img class="pause" src="img/play-btn.svg" alt="pause button">
                                <img class="play" src="img/play-btn2.svg" alt="play button">
                            </a>
                            <a class="forward-btn">
                                <img src="img/forward btn.svg" alt="forward button">
                            </a>
                            <a class="rewind-btn">
                                <img src="img/rewind btn.svg" alt="forward button">
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
// Play/pause toggle //
$('.play-toggle').click(function () {
    if ($('.acc-wrap').hasClass("ui-state-active")) {
        if (player.paused()) {
            player.play();
            $('.play').css({
                "visibility": "visible",
            });
            $('.pause').css({
                "visibility": "hidden",
            });
        } else {
            player.pause();
            $('.play').css({
                "visibility": "hidden",
            });
            $('.pause').css({
                "visibility": "visible",
            });
        };
    };
});

I would recommend looking into learning more about Scope and Classes / Object Prototypes .我建议您了解更多有关ScopeClasses / Object Prototypes的信息。

The problem with you jQuery code right away is that there are multiple .play-toggle classes and you currently have no way of identifying the correct video in which this play button belongs to.您的 jQuery 代码的问题是有多个.play-toggle类,您目前无法识别该播放按钮所属的正确视频。

You can begin to scope if by passing the event param into your click handler to identify which item is actually being clicked on您可以开始 scope 如果通过将event参数传递到您的点击处理程序来识别实际点击了哪个项目

$('.play-toggle').click(function (event) {
    console.log(event)
})

From here you can identify parent container find the video and hook into the play pause functions.从这里您可以识别父容器找到视频并连接到播放暂停功能。 However I would recommend another approach.但是我会推荐另一种方法。

Step 1: Create a VideoPlayer Class to house all VideoPlayer functionality第 1 步:创建一个 VideoPlayer Class 以容纳所有 VideoPlayer 功能

class VideoPlayer {
    constructor(el) {
        this._el = el
        this._video = el.querySelector('video')
        this._playToggle = el.querySelector('.play-toggle')

        this.setupEventListeners()
    }

    setupEventListeners() {
        this._playToggle.addEventListener('click',() => {
            console.log(`play button clicked on element id ${this._el.id}`)
        })
    }
}

If you are not sure about classes please read up on them, here we are scoping the video player elements and saving them as variables and then initialising an event listener that listens for you click event.如果您不确定类,请阅读它们,这里我们将视频播放器元素定义为变量并将它们保存为变量,然后初始化一个事件侦听器来侦听您的点击事件。 You would store all your video player functionality inside this class您将在此 class 中存储所有视频播放器功能

Step 2: Iterate through the page and identify all potential video players第 2 步:遍历页面并识别所有潜在的视频播放器

let videoPlayers = document.querySelectorAll('.video-player')

Here we know all video players are wrapped in the parent class video-player if you console.log(videoPlayers) you should see an array of elements在这里,我们知道所有视频播放器都包含在父 class video-player中,如果您使用console.log(videoPlayers) ,您应该会看到一系列元素

Step 3. Create an instance of the VideoPlayer class for each element步骤 3. 为每个元素创建一个 VideoPlayer class 的实例

videos.forEach(v => new VideoPlayer(v))

This will create a new instance of the VidePlayer class for each item you have inside videoPlayers, now if you click a play button you should see a log in your console of play button clicked on element id (whatever id)这将为您在 videoPlayers 中的每个项目创建一个新的 VidePlayer class 实例,现在如果您单击播放按钮,您应该会在您的控制台中看到play button clicked on element id (whatever id)

Once again inside your video player class I would house all the logic for your buttons, here you can hook into whatever HTML5 video events you want to customize your video player.再次在您的视频播放器 class 中,我将包含您按钮的所有逻辑,在这里您可以连接到您想要自定义视频播放器的任何 HTML5 视频事件。

Please note that you will have to handle removing your event listeners on your own, this is not a complete answer but should give you enough information on how to solve your problem请注意,您必须自己处理删除事件侦听器,这不是一个完整的答案,但应该为您提供有关如何解决问题的足够信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM