如何在 Android 上自动播放 HTML5 mp4 视频?

发布于 2025-01-01 02:37:38 字数 787 浏览 3 评论 0原文

我通过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 技术交流群。

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

发布评论

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

评论(15

戏舞 2025-01-08 02:37:39

不要单独使用“mute”,使用 [muted]="true" 例如以下代码:

<video id="videoPlayer" [muted]="true" autoplay playsinline loop style="width:100%; height: 100%;">
<source type="video/mp4" src="assets/Video/Home.mp4">
<source type="video/webm" src="assets/Video/Home.webm">
</video>

我在更多 Android 和 ios 中进行测试

don't use "mute" alone, use [muted]="true" for example following code:

<video id="videoPlayer" [muted]="true" autoplay playsinline loop style="width:100%; height: 100%;">
<source type="video/mp4" src="assets/Video/Home.mp4">
<source type="video/webm" src="assets/Video/Home.webm">
</video>

I test in more Android and ios

心如荒岛 2025-01-08 02:37:39

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.

初熏 2025-01-08 02:37:38

您可以将“静音”和“自动播放”属性添加在一起,以启用 Android 设备的自动播放功能。

例如

<video id="video" class="video" autoplay muted >

You can add the 'muted' and 'autoplay' attributes together to enable autoplay for android devices.

e.g.

<video id="video" class="video" autoplay muted >

情感失落者 2025-01-08 02:37:38

我使用了以下代码:

// get the video
var video = document.querySelector('video');
// use the whole window and a *named function*
window.addEventListener('touchstart', function videoStart() {
  video.play();
  console.log('first touch');
  // remove from the window and call the function we are removing
  this.removeEventListener('touchstart', videoStart);
});

似乎没有办法再自动启动了。

这使得他们第一次触摸屏幕时就会播放视频。它还会在第一次运行时删除自身,以便您可以避免多个侦听器添加。

I used the following code:

// get the video
var video = document.querySelector('video');
// use the whole window and a *named function*
window.addEventListener('touchstart', function videoStart() {
  video.play();
  console.log('first touch');
  // remove from the window and call the function we are removing
  this.removeEventListener('touchstart', videoStart);
});

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.

长梦不多时 2025-01-08 02:37:38

Android 实际上有一个 API 可以实现这一点!该方法是 setMediaPlaybackRequiresUserGesture()。我在深入研究视频自动播放并尝试了很多来自 SO 的黑客攻击后发现了它。以下是 blair vanderhoof 的示例:

package com.example.myProject;

import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings;

public class myProject extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html");

        WebSettings ws = super.appView.getSettings();
        ws.setMediaPlaybackRequiresUserGesture(false);
    }
}

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:

package com.example.myProject;

import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings;

public class myProject extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html");

        WebSettings ws = super.appView.getSettings();
        ws.setMediaPlaybackRequiresUserGesture(false);
    }
}
无声无音无过去 2025-01-08 02:37:38

我认为自动播放在 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.

度的依靠╰つ 2025-01-08 02:37:38

在 Android 4.4 及更高版本中,只要 HTML5 视频组件位于您自己的 WebView 中,您就可以消除对用户手势的需要。

webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);

要使视频自动播放,您仍然需要向视频元素添加自动播放:

<video id='video' controls autoplay>
  <source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' >
</video>

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

webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);

To get the video to autoplay, you'd still need to add autoplay to the video element:

<video id='video' controls autoplay>
  <source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' >
</video>
嘿嘿嘿 2025-01-08 02:37:38

重要提示:请注意,如果在 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

听不够的曲调 2025-01-08 02:37:38

与 KNaito 的答案类似,以下内容对我有用

function simulateClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  var cb = document.getElementById('player'); 
  var canceled = !cb.dispatchEvent(event);
  if (canceled) {
    // A handler called preventDefault.
    alert("canceled");
  } else {
    // None of the handlers called preventDefault.
    alert("not canceled");
  }
}

similar to KNaito's answer, the following does the trick for me

function simulateClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  var cb = document.getElementById('player'); 
  var canceled = !cb.dispatchEvent(event);
  if (canceled) {
    // A handler called preventDefault.
    alert("canceled");
  } else {
    // None of the handlers called preventDefault.
    alert("not canceled");
  }
}
嘿看小鸭子会跑 2025-01-08 02:37:38

在Android 4.1和4.2中,我使用以下代码。

    evt.initMouseEvent( "click", true,true,window,0,0,0,0,0,false,false,false,false,0, true );
    var v = document.getElementById("video");
    v.dispatchEvent(evt);

其中 html 是

    <video id="video" src="sample.mp4" poster="image.jpg" controls></video>

这效果很好。但在Android 4.4中,它不起作用。

In Android 4.1 and 4.2, I use the following code.

    evt.initMouseEvent( "click", true,true,window,0,0,0,0,0,false,false,false,false,0, true );
    var v = document.getElementById("video");
    v.dispatchEvent(evt);

where html is

    <video id="video" src="sample.mp4" poster="image.jpg" controls></video>

This works well. But In Android 4.4, it does not work.

忆伤 2025-01-08 02:37:38

这是 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

谁的年少不轻狂 2025-01-08 02:37:38
<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>
<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>
心舞飞扬 2025-01-08 02:37:38

我简化了 JavaScript 来触发视频开始。

 var bg = document.getElementById ("bg"); 
 function playbg() {
   bg.play(); 
 }
<video id="bg" style="min-width:100%; min-height:100%;"  playsinline autoplay loop muted onload="playbg(); "><source src="Files/snow.mp4" type="video/mp4"></video>
</td></tr>
</table>

*“Files/snow.mp4”只是示例网址

I simplified the Javascript to trigger the video to start.

 var bg = document.getElementById ("bg"); 
 function playbg() {
   bg.play(); 
 }
<video id="bg" style="min-width:100%; min-height:100%;"  playsinline autoplay loop muted onload="playbg(); "><source src="Files/snow.mp4" type="video/mp4"></video>
</td></tr>
</table>

*"Files/snow.mp4" is just sample url

你好,陌生人 2025-01-08 02:37:38

可以添加muted标签。

<video autoplay muted>
  <source src="video.webm" type="video/webm" />
  <source src="video.mp4" type="video/mp4" />
</video>

参考https://developers.google.com/web/updates/2016/07 /自动播放

Can add muted tag.

<video autoplay muted>
  <source src="video.webm" type="video/webm" />
  <source src="video.mp4" type="video/mp4" />
</video>

reference https://developers.google.com/web/updates/2016/07/autoplay

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