如何在 Android 上自动播放 HTML5 mp4 视频?
我通过asp.net开发了一个移动页面来播放mp4视频。
我知道 iOS 已禁用自动播放功能以最大限度地减少用户带宽,那么我该如何 在 Android 上自动播放 HTML5 mp4 视频?
我已经在 HTML5 代码中添加了自动播放功能,但它不起作用。
以下是我的代码:
<video autoplay controls id='video1' width='100%' poster='images/top_icon.png' webkitEnterFullscreen poster preload='true'>
<source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2' >
</video>
此外,我还修复了用户单击图像叠加层可以播放视频的问题。 谢谢卡蒂,
这是代码:
<script type="text/javascript">
$(document).ready(function() {
$("#video1").bind("click", function() {
var vid = $(this).get(0);
if (vid.paused) { vid.play(); }
else { vid.pause(); }
});
});
</script>
谢谢
乔
I had developed a mobile page by asp.net to play mp4 video.
I know iOS had disabled the autoplay function to minimize user bandwidth, so how can i
autoplay HTML5 mp4 video on Android ?
I had already put autoplay in HTML5 code, but it doesn't work.
The following is my code:
<video autoplay controls id='video1' width='100%' poster='images/top_icon.png' webkitEnterFullscreen poster preload='true'>
<source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2' >
</video>
Moreover, I had fixed the problem that user click on the image overlay can play the video.
Thanks Karthi
here are the code:
<script type="text/javascript">
$(document).ready(function() {
$("#video1").bind("click", function() {
var vid = $(this).get(0);
if (vid.paused) { vid.play(); }
else { vid.pause(); }
});
});
</script>
Thanks
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
不要单独使用“mute”,使用 [muted]="true" 例如以下代码:
我在更多 Android 和 ios 中进行测试
don't use "mute" alone, use [muted]="true" for example following code:
I test in more Android and ios
Chrome 已禁用它。 https://bugs.chromium.org/p/chromium/issues/detail ?id=159336
甚至 jQuery play() 也被阻止。他们希望用户启动它,以便节省带宽。
Chrome has disabled it. https://bugs.chromium.org/p/chromium/issues/detail?id=159336
Even the jQuery play() is blocked. They want user to initiate it so bandwidth can be saved.
您可以将“静音”和“自动播放”属性添加在一起,以启用 Android 设备的自动播放功能。
例如
You can add the 'muted' and 'autoplay' attributes together to enable autoplay for android devices.
e.g.
我使用了以下代码:
似乎没有办法再自动启动了。
这使得他们第一次触摸屏幕时就会播放视频。它还会在第一次运行时删除自身,以便您可以避免多个侦听器添加。
I used the following code:
There doesn't seem to be a way to auto-start anymore.
This makes it so that the first time they touch the screen the video will play. It will also remove itself on first run so that you can avoid multiple listeners adding up.
Android 实际上有一个 API 可以实现这一点!该方法是 setMediaPlaybackRequiresUserGesture()。我在深入研究视频自动播放并尝试了很多来自 SO 的黑客攻击后发现了它。以下是 blair vanderhoof 的示例:
Android actually has an API for this! The method is setMediaPlaybackRequiresUserGesture(). I found it after a lot of digging into video autoplay and a lot of attempted hacks from SO. Here's an example from blair vanderhoof:
我认为自动播放在 Android 上不起作用,但播放视频可能会非常棘手。我建议阅读这篇文章: 让 HTML5 视频在 Android 手机上运行。
I don't think autoplay works on Android, but getting a video to play can be annoyingly tricky. I suggest giving this article a read: Making HTML5 Video work on Android phones.
在 Android 4.4 及更高版本中,只要 HTML5 视频组件位于您自己的 WebView 中,您就可以消除对用户手势的需要。
要使视频自动播放,您仍然需要向视频元素添加自动播放:
In Android 4.4 and above you can remove the need for a user gesture so long as the HTML5 Video component lives in your own WebView
To get the video to autoplay, you'd still need to add autoplay to the video element:
重要提示:请注意,如果在 Chrome 设置中启用了 Google Chrome 数据保护程序,则自动播放功能将被禁用。
Important Note: Be aware that if Google Chrome Data Saver is enabled in Chrome's settings, then Autoplay will be disabled.
自动播放仅在第二次时有效。
在 android 4.1+ 上,你必须有某种用户事件才能让第一个 play() 工作。一旦发生这种情况,自动启动就会起作用。
这样用户就知道他们正在使用带宽。
还有另一个问题可以回答这个问题。
使用 android 4 浏览器自动启动 html5 视频
Autoplay only works the second time through.
on android 4.1+ you have to have some kind of user event to get the first play() to work. Once that has happened then autostart works.
This is so that the user is acknowledging that they are using bandwidth.
There is another question that answers this .
Autostart html5 video using android 4 browser
与 KNaito 的答案类似,以下内容对我有用
similar to KNaito's answer, the following does the trick for me
在Android 4.1和4.2中,我使用以下代码。
其中 html 是
这效果很好。但在Android 4.4中,它不起作用。
In Android 4.1 and 4.2, I use the following code.
where html is
This works well. But In Android 4.4, it does not work.
这是 PhoneGap 的一个插件,它解决了我的问题:
https://build.phonegap.com/plugins/1031
我只是将其包含在我的配置中。 XML
Here is a plugin for PhoneGap which solved the problem for me:
https://build.phonegap.com/plugins/1031
I simply included it in my config.xml
我简化了 JavaScript 来触发视频开始。
*“Files/snow.mp4”只是示例网址
I simplified the Javascript to trigger the video to start.
*"Files/snow.mp4" is just sample url
可以添加
muted
标签。参考https://developers.google.com/web/updates/2016/07 /自动播放
Can add
muted
tag.reference https://developers.google.com/web/updates/2016/07/autoplay