如何使用 MediaElements.JS 实现插播广告?
我做了预卷,我也在这里提问。按下原始视频的播放按钮后如何开始广告?
我想也许有一些广告控件或其他东西,但我没有找到。
这是我的代码:
$('iframe').attr('allowfullscreen', '');
$("iframe, object").each(function(i) {
var thisYT = $(this);
if (thisYT.is("iframe")) {
var src = thisYT.attr('src');
} else {
var src = thisYT.attr('data');
}
var width = thisYT.attr('width');
var height = thisYT.attr('height');
var YTID = getId(src);
if (YTID != "error") {
thisYT.replaceWith('<video id="' + YTID + '" width="' + width + '" height="' + height + '" controls="control" preload="none"><source src="https://www.youtube.com/watch?v=' + YTID + '" type="video/youtube" /></video>');
}
var player = new MediaElementPlayer(YTID, {
playsinline: true,
features: ['playpause','current','progress','duration','volume','fullscreen','vast'],
vastAdTagUrl: 'url',
adsPrerollAdEnableSkip: 4,
adsPrerollAdSkipSeconds: 10,
success: function(mediaElement, originalNode, instance) {
console.log(mediaElement, originalNode, instance);
}
});
});
function getId(url) {
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return 'error';
}
}