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

cgyqldqp  于 11个月前  发布在  Node.js
关注(0)|答案(3)|浏览(97)

我使用的videojs-playlist插件沿着与谷歌的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();

字符串
我尝试过从“结束”事件处理程序中手动调用广告的各种方法,例如再次调用requestAds

const _this = this;
this.player.on( 'ended', function(){
    /* some other stuff */
    _this.player.ima.requestAds();
});


这 * 确实 * 在我想要的地方播放广告,但是
1.这打破了播放列表的“自动前进”设置(下一个视频不开始播放时,广告完成),和
1.这使播放器进入“广告显示”模式(擦除器不可用等)。
有没有一种简单的方法可以编程地说“现在播放广告”?我试过,没有快乐,使用ima插件和它所依赖的contrib-ads插件提供的所有看似适用的方法。我承认这是我第一次不得不处理运行广告的视频,所以我有点菜鸟。

j2datikz

j2datikz1#

我试图做同样的事情。就像你一样,我在事件上调用player.ima.requestAds()时失败了。我挖得更深,我能想到的最好的就是我在下面分享的。
根据videojs-ima API,你必须使用setContentWithAdTag方法来切换播放器内容。在我们的例子中,它是player.playlist.next方法。
我将videojs-ima examples中的代码与原始的playlist.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-playlist-ui,所以在我的情况下,需要更改名为switchPlaylistItem_的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();
};


PlaylistMenuItem的原型应该在 * 初始化播放器之前 * 修改。
这个解决方案是有效的,但感觉很黑客,所以如果有人能想出更干净的东西,请分享!

mwecs4sa

mwecs4sa2#

最后我派生了videojs-playlist,并添加了覆盖player.src方法的选项。请随意使用它:
fw-videojs-playlist
关于如何使用它的详细信息都在github readme中(包括一个ima.setContentWithAdTag示例)

rjee0c15

rjee0c153#

我可以成功地做到这一点,

videojs-ima: 2.2.0
videojs-playlist: 5.1.0

字符串
你只需要听beforeplaylistitem
类似这样的工作:

player.on('beforeplaylistitem', (event, item) => {
    const adtag = 'youradtag';
    player.ima.setContentWithAdTag(item.sources, adtag);
    player.ima.requestAds();
});

相关问题