在 HTML5 视频上插入广告
如何在主视频播放之前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个快速即兴启动的解决方案,至少应该为您指明正确的方向。这为您提供了一个带有 init 方法的单例,调用该方法时会在特定视频元素上设置预卷。
不过,还有很多事情没有在这里讨论。例如,如果您设置在开始播放视频时调用 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.
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.
抱歉,我现在无法测试此代码,但理论上这应该可行。
Sorry I can't test this code right now but in theory this should work.
您可能想看看 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
@natlee75 对我来说这不起作用
我把它改成这样:
@natlee75 For me this didn't work
I changed It to this:
这个问题是 8 年前提出的,多年来情况发生了变化,现在我们有了 VAST、VPAID、VMAP 和 VAST 等协议>MRAID。
此处了解有关它们的信息。
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.