Javascript 如何在 iOS 上自动播放 HTML5 <video> 和 <audio> 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32945273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:24:35  来源:igfitidea点击:

How to autoplay HTML5 <video> and <audio> tags on iOS ?

javascriptioshtmlaudiovideo

提问by Jean-Baptiste Bouhier

My team and I are tying to make a web application autoplay HTML5 video/audio tags on iPad. So far no luck after many attempts with CSSand JavaScript.

我和我的团队正在尝试在iPad上制作一个自动播放 HTML5 视频/音频标签的网络应用程序。到目前为止,在使用CSSJavaScript进行了多次尝试后,仍然没有运气。

The question is: What other option is there to make it autoplay ?

问题是:还有什么其他选择可以让它自动播放?

I'm thinking native app with integrated browser with iOS APIcalls to simulate a touch/clickand thus making the video/audio tags autoplay.

我正在考虑使用带有iOS API调用的集成浏览器的本机应用程序来模拟触摸/点击,从而使视频/音频标签自动播放。

Is it possible and if yes, how and with which iOS version ?

是否可能,如果是,如何以及使用哪个 iOS 版本?

Thanks

谢谢

回答by Omidam81

you can't auto-play videos and audios on iOS in normal mode, you may use trick like simulate user input to auto-play videos and audios.

您无法在正常模式下在 iOS 上自动播放视频和音频,您可以使用模拟用户输入等技巧来自动播放视频和音频。

Safari HTML5 Audio and Video Guide

Safari HTML5 音频和视频指南

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.

在 iOS 上的 Safari(适用于所有设备,包括 iPad)中,用户可能使用蜂窝网络并按数据单位收费,预加载和自动播放被禁用。在用户启动之前不会加载任何数据。这意味着 JavaScript play() 和 load() 方法在用户启动播放之前也处于非活动状态,除非 play() 或 load() 方法由用户操作触发。换句话说,用户启动的播放按钮有效,但 onLoad="play()" 事件无效。

This plays the movie: <input type="button" value="Play" onclick="document.myMovie.play()">

这是播放电影: <input type="button" value="Play" onclick="document.myMovie.play()">

This does nothing on iOS: <body onload="document.myMovie.play()">

这在 iOS 上没有任何作用: <body onload="document.myMovie.play()">

回答by Raniys

Have you found solution for autoplay video on iOS device? If not, I've just find a solution here, you can use WeChat of iPhone to open it(just support WeChat of iPhone but not support iPad WeChat), it use video.js player, related code as below:

您是否找到了在 iOS 设备上自动播放视频的解决方案?如果不行,我这里也只找到了解决办法,可以用iPhone的微信打开(只支持iPhone的微信,不支持iPad的微信),使用video.js播放器,相关代码如下:

<video id="example_video_1" class="video-js vjs-default-skin" controls autoplay preload="auto" webkit-playsinline="true" x-webkit-airplay="true" width="750" height="460" poster="" data-setup="{}">

It also has solution for support video play without full screen on iOS/Android/PC.

它还具有支持在 iOS/Android/PC 上不全屏播放视频的解决方案。