Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js VIDEOJS:在每个播放列表项之前播放预滚动添加_Node.js_Video_Video.js_Google Ima - Fatal编程技术网

Node.js VIDEOJS:在每个播放列表项之前播放预滚动添加

Node.js VIDEOJS:在每个播放列表项之前播放预滚动添加,node.js,video,video.js,google-ima,Node.js,Video,Video.js,Google Ima,我正在使用videojs播放列表插件和谷歌的videojs ima插件。一切都很顺利,除了在第一个视频之前我只收到了一个预装广告。我希望播放列表中的每个视频前都有一个。 基本设置是样板,但仅供参考: this.player = videojs('currentvideo', { autoplay : true, fluid : true }); this.player.playlist(this.playlist); this.player.playlist.autoadvance(5); c

我正在使用videojs播放列表插件和谷歌的videojs ima插件。一切都很顺利,除了在第一个视频之前我只收到了一个预装广告。我希望播放列表中的每个视频前都有一个。 基本设置是样板,但仅供参考:

this.player = videojs('currentvideo', { autoplay : true, fluid : true });
this.player.playlist(this.playlist);
this.player.playlist.autoadvance(5);

const skippable_linear = {google's test ad};
const options = {
    id: 'currentvideo',
    adTagUrl: skippable_linear,
    debug : true
};

this.player.ima(
    options
);
this.player.ima.requestAds();
我尝试过从“结束”事件处理程序内部手动调用ads的各种方法,例如再次调用
requestAds

const _this = this;
this.player.on( 'ended', function(){
    /* some other stuff */
    _this.player.ima.requestAds();
});
这确实在我想要的地方播放广告,但是

  • 这会中断播放列表的“自动前进”设置(广告完成后,下一个视频不会开始播放),并且
  • 这将使播放器进入“广告显示”模式(洗涤器不可用等)

  • 有没有一种简单的方式可以通过编程说“现在播放广告”?我尝试过使用ima插件和它所依赖的contrib ads插件公开的所有看似适用的方法,但没有任何乐趣。我要承认,这是我第一次不得不处理播放广告的视频,所以我有点傻。

    我也在尝试做同样的事情。就像你一样,我在事件上调用
    player.ima.requestAds()
    时失败了。我挖得更深了,我能想出的最好的办法就是我下面分享的东西

    根据videojs ima,您必须使用
    setContentWithAdTag
    方法,而不是使用任何方法来切换播放器内容。在我们的例子中,它是
    player.playlist.next
    方法

    我将中找到的代码与原始的
    播放列表相结合。next
    编写我自己的
    next

    然后,我非常残忍地推翻了原来的插件方法

    代码如下:

    player.playlist(myPlayilst);
    player.playlist.autoadvance(2);
    player.playlistUi(); //videojs-playlist-ui
    
    player.ima({
        id: 'video5',
        adTagUrl: 'thy adserver request'
    });
    
    //override playlist.next
    player.playlist.next = function(){
    
        var nextIndex = 0,
            playlist = this.player_.playlist,
            list = this.player_.playlist();
    
    //everything below is copied directly from the original `next` (except for the "//load with ad")
    
        // Repeat
        if (playlist.repeat_) {
            nextIndex = playlist.currentIndex_ + 1;
            if (nextIndex > list.length - 1) {
                nextIndex = 0;
            }
    
        } else {
            // Don't go past the end of the playlist.
            nextIndex = Math.min(playlist.currentIndex_ + 1, list.length - 1);
        }
    
        // Make the change
        if (nextIndex !== playlist.currentIndex_) {
    
        //load with ad
            this.player_.playlist.currentItem(nextIndex);
            this.player_.ima.setContentWithAdTag(
                this.player_.playlist.currentItem(),
                null,
                true);
    
            this.player_.ima.requestAds();
        /////
    
            return list[playlist.currentItem()];
        }
    }
    
    您可能需要覆盖更改当前播放的其他方法,如
    playlist.previous

    我使用的是
    videojs播放列表ui
    ,因此在我的例子中,有必要更改名为
    switchplaylitem\uucode>的onclick处理程序。我用了一些很好的蛮力来做到这一点:

    videojs.getComponent('PlaylistMenuItem').prototype.switchPlaylistItem_ = function(e){
        this.player_.playlist.currentItem(this.player_.playlist().indexOf(this.item));
        this.player_.ima.setContentWithAdTag(
            this.player_.playlist.currentItem(),
            null,
            true);
        this.player_.ima.requestAds();
    };
    
    playlymenuitem
    的原型应在初始化播放器之前进行更改


    这个解决方案是可行的,但感觉很粗糙,所以如果有人能想出更干净的东西,请分享

    我最终分叉了
    videojs播放列表
    ,并添加了覆盖
    player.src
    方法的选项。请随意使用它:


    关于如何使用它的详细信息都在github自述文件中(包括带有
    ima.setContentWithAdTag
    的示例)

    Ooh,这看起来很有希望!我最终只是制作了自己的播放列表(放弃了播放列表插件),并在每个视频之后调用这个.player.dispose(),然后重新创建播放器。我来看看这个。我的解决方案并不理想。老实说,我认为最好自己写播放列表。最好的方法是打开videojs播放列表,然后根据您的目的更改
    next
    prev
    。顺便说一句——很抱歉,您是一个追根究底的人——但您能投票支持或接受我的答案吗?我将不胜感激:)