在 HTML5 视频上插入广告

发布于 2024-10-21 10:19:15 字数 625 浏览 1 评论 0原文

如何在主视频播放之前在 html5 视频标签上插入广告?有没有开源工具可以让这变得更容易?有什么参考资料可以指导我吗?

它正在使用此代码:

<script type="text/javascript">
   // listener function changes src
   function myNewSrc() {
      var myVideo = document.getElementsByTagName('video')[0];
      myVideo.src="../main.webm";
      myVideo.load();
      myVideo.play();
    }

   // function adds listener function to ended event -->

    function myAddListener(){
     var myVideo = document.getElementsByTagName('video')[0];
     myVideo.addEventListener('ended',myNewSrc,false);
    }
</script>

但当它播放第二个代码时我不能。它显示了海报。如何去掉海报?

How can I insert ads on html5 video tag before the main video plays? Is there any open source tools to make this easier? Is there any reference that can guide me there?

It is working with this code:

<script type="text/javascript">
   // listener function changes src
   function myNewSrc() {
      var myVideo = document.getElementsByTagName('video')[0];
      myVideo.src="../main.webm";
      myVideo.load();
      myVideo.play();
    }

   // function adds listener function to ended event -->

    function myAddListener(){
     var myVideo = document.getElementsByTagName('video')[0];
     myVideo.addEventListener('ended',myNewSrc,false);
    }
</script>

but I can't when it play the second one. It shows the poster. How do I get rid of the poster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

微凉徒眸意 2024-10-28 10:19:15

这是一个快速即兴启动的解决方案,至少应该为您指明正确的方向。这为您提供了一个带有 init 方法的单例,调用该方法时会在特定视频元素上设置预卷。

var adManager = function () {
    var vid = document.getElementById("myVid"),
        adSrc = "videos/epic_rap_battles_of_history_16_adolf_hitler_vs_darth_vader_2_1280x720.mp4",
        src;

    var adEnded = function () {
        vid.removeEventListener("ended", adEnded, false);
        vid.src = src;
        vid.load();
        vid.play();
    };

    return {
        init: function () {
            src = vid.src;
            vid.src = adSrc;
            vid.load();
            vid.addEventListener("ended", adEnded, false);
        }
    };
}();

不过,还有很多事情没有在这里讨论。例如,如果您设置在开始播放视频时调用 init 方法,则需要保留一个标志来指示是否正在播放广告,以便播放处理程序在您从视频转换时不会执行任何操作广告到内容(在 load() 调用之后需要一个“播放”事件才能获得无缝播放)。

我们在视频播放项目中使用类似的方法,并且大多数视频广告服务都针对基于 HTML 的视频播放(而不是 Flash 视频播放)执行类似的操作。

它相对简单,但您只需确保跟踪何时应触发事件回调以及何时添加和删除这些回调。

另一件需要考虑的事情是“结束”事件的不可靠性。我还没有弄清楚它何时何地在哪些平台上持续触发,但这是一个众所周知的问题。一种可能的解决方案是使用“timeupdate”,并测试“currentTime”属性是否比“duration”属性少一秒左右,这样您就知道您正处于视频的结尾处。

This is a quick off the cuff start of a solution that should at least point you in the right direction. This gives you a singleton with an init method that when called sets up a preroll on a particular video element.

var adManager = function () {
    var vid = document.getElementById("myVid"),
        adSrc = "videos/epic_rap_battles_of_history_16_adolf_hitler_vs_darth_vader_2_1280x720.mp4",
        src;

    var adEnded = function () {
        vid.removeEventListener("ended", adEnded, false);
        vid.src = src;
        vid.load();
        vid.play();
    };

    return {
        init: function () {
            src = vid.src;
            vid.src = adSrc;
            vid.load();
            vid.addEventListener("ended", adEnded, false);
        }
    };
}();

There are a number of things that aren't covered here, though. For instance, if you set the init method to be called when you start playing the video, you'll need to keep a flag that indicates whether there's an ad playing so that the play handler won't do anything when you're transitioning from the ad to the content (which requires a "play" event after the load() call in order to get the seamless playback).

We use something similar in our video playing project, and most of the video ad services out there do something like this for HTML based video playback (as opposed to Flash video playback).

It's relatively straightforward, but you just have to make sure to keep track of when event callbacks should be fired and when to add and remove those callbacks.

Another thing to consider is the unreliability of the "ended" event. I haven't yet figured out when and on which platforms it consistently fires, but it's a fairly well known problem. A possible solution is to use "timeupdate" instead and test whether the "currentTime" property is somewhere around a second less than the "duration" property so you know you're right at the end of the video.

不可一世的女人 2024-10-28 10:19:15

抱歉,我现在无法测试此代码,但理论上这应该可行。

<script>
// you will want to do checking here to see if the browser supports the video element
document.getElementById('video').addEventListener('ended', function()
{
    // the ad finished playing so update the src attribute to the real video
    document.getElementById('video').src = 'mainvideo.webm';
});
</script>

<video id="video" src="ad.webm">
</video>

Sorry I can't test this code right now but in theory this should work.

<script>
// you will want to do checking here to see if the browser supports the video element
document.getElementById('video').addEventListener('ended', function()
{
    // the ad finished playing so update the src attribute to the real video
    document.getElementById('video').src = 'mainvideo.webm';
});
</script>

<video id="video" src="ad.webm">
</video>
赤濁 2024-10-28 10:19:15

您可能想看看 Popcorn.js 可以做什么。它允许您与 Html5 视频和覆盖文本以及许多其他很酷的东西进行交互:

http://popcornjs.org/documentation< /a>

You may want to look at what Popcorn.js can do. It allows you to interact with Html5 video and overlay text and a lot of other cool things:

http://popcornjs.org/documentation

云朵有点甜 2024-10-28 10:19:15

@natlee75 对我来说这不起作用
我把它改成这样:

$( document ).ready(function() {
var adManager = function () {
    var vid = document.getElementById("vid1564730217"),
        adSrc = "http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4",
        src;

    var adEnded = function () {
        vid.removeEventListener("ended", adEnded, false);
        vid.src = src;
        vid.load();
        vid.play();
    };
    return {
        init: function () {
            src = vid.src;
            vid.src = adSrc;
            vid.load();
            vid.addEventListener("ended", adEnded, false);
			
        }
    };
}().init();		
		});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="vid1564730217" src="http://techslides.com/demos/sample-videos/small.mp4" width="100%" style="max-height:600px;" poster="http://orperry.com/sample/wp-content/uploads/2015/12/sample-logo.png" controls>
	<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

@natlee75 For me this didn't work
I changed It to this:

$( document ).ready(function() {
var adManager = function () {
    var vid = document.getElementById("vid1564730217"),
        adSrc = "http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4",
        src;

    var adEnded = function () {
        vid.removeEventListener("ended", adEnded, false);
        vid.src = src;
        vid.load();
        vid.play();
    };
    return {
        init: function () {
            src = vid.src;
            vid.src = adSrc;
            vid.load();
            vid.addEventListener("ended", adEnded, false);
			
        }
    };
}().init();		
		});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="vid1564730217" src="http://techslides.com/demos/sample-videos/small.mp4" width="100%" style="max-height:600px;" poster="http://orperry.com/sample/wp-content/uploads/2015/12/sample-logo.png" controls>
	<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

你爱我像她 2024-10-28 10:19:15

您是否曾经在线观看过视频并看到横幅广告显示在
视频顶部?或者观看视频并看到中途出现广告
通过?使用时,富媒体广告占据屏幕怎么样?
移动应用程序?

这个问题是 8 年前提出的,多年来情况发生了变化,现在我们有了 VASTVPAIDVMAPVAST 等协议>MRAID。

此处了解有关它们的信息。

Have you ever watched a video online and seen a banner ad displayed on
top of the video? Or watched a video and seen an ad appear halfway
through? How about a rich media ad take over the screen when using a
mobile app?

This question was asked 8 years ago, Things have changed over the years and now we have protocols like VAST, VPAID, VMAP, and MRAID.

Read about them here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文