YouTube iframe 嵌入无法在 Android 上自动播放 [英] YouTube iframe embeds cannot autoplay on Android

查看:92
本文介绍了YouTube iframe 嵌入无法在 Android 上自动播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Android 应用中使用 webView 来加载 YouTube iframe 播放器和自动播放视频.它在三星 Galaxy S2 上运行良好S3,但在三星 Galaxy S4 上运行时,尝试自动播放时总是导致灰屏.

在 Galaxy S4 上,无需自动播放即可正常工作,需要用户操作(单击)才能开始播放(如果在 playerVars 中添加autoplay:1",则不会发生任何事情).

我尝试在 onPlayerReady() 中调用 player.playVideo(),结果出现灰屏:

LogCat 还显示一个奇怪的错误消息:

E/IMGSRV(17004): :0: GetPTLAFormat: 无效格式

自动播放失败时.我不知道这条消息是关于什么的;我用谷歌搜索了一下,什么也没找到.

这是WebView的Android代码:

WebView wv = (WebView) findViewById(R.id.webview);WebSettings websettings = wv.getSettings();websettings.setJavaScriptEnabled(true);websettings.setDomStorageEnabled(true);websettings.setDatabaseEnabled(true);wv.loadUrl(strUrl);wv.setWebViewClient(new WebViewClient());wv.setWebChromeClient(new WebChromeClient());wv.setPadding(0, 0, 0, 0);wv.getSettings().setLoadWithOverviewMode(true);wv.getSettings().setUseWideViewPort(true);wv.setVerticalScrollBarEnabled(false);wv.setHorizo​​ntalScrollBarEnabled(false);

这是一个已知问题,还是有任何解决方案可以自动播放视频?谢谢!

解决方案

我认为禁用自动播放正在成为一种标准".

顺便说一句,你可以试试 这个

由于链接已失效,我在 https://lumi.do/p/04x626i1ngvht/how-to-autoplay-html5-video-on-android:

<块引用>

如何在 Android 上自动播放"HTML5 视频
由 Mathias Desloges 于 2011 年 5 月 19 日在 Labs 撰写

您是否尝试过在 Android 设备上使用新的 HTML5 视频标签?
我们遇到了一个非常棘手的问题.
让我们设置上下文,我们希望我们的视频在页面加载完成后立即开始播放.
根据 HTML5 规范,我们应该使用autoplay"属性,但它在 Android 中不起作用.所以我们尝试了一个小脚本,在页面加载后调用播放函数:

函数回调() { document.querySelector('video').play();}window.addEventListener("加载", 回调, false);

<块引用>

这很好地再现了自动播放"属性的行为经典的桌面浏览器,但在 Android 浏览器上却失败了.
我们试图将先前定义的callback()"函数附加到任意节点上的点击事件,视频开始播放良好on点击"(正常行为).同时继续处理页面 html代码,我们找到了解决方案!

不要问我为什么,但是添加海报"属性后它就可以了!

I'm using a webView in my Android app to load YouTube iframe player and auto play videos. It works fine on Samsung Galaxy S2 & S3, but when runs on Samsung Galaxy S4, it always results in gray screen when trying to auto play.

On Galaxy S4, it works fine without autoplay, needs user action(click) to start playing (Nothing happened if adding "autoplay:1" in playerVars).

I tried to call player.playVideo() in onPlayerReady(), it resulted in this gray screen:

The LogCat also shows a weird error message:

E/IMGSRV(17004): :0: GetPTLAFormat: Invalid format

when failed to autoplay. I don't know what this message is about; I've googled it and found nothing.

Here's the Android code of WebView:

WebView wv = (WebView) findViewById(R.id.webview);
WebSettings websettings = wv.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setDomStorageEnabled(true);
websettings.setDatabaseEnabled(true);
wv.loadUrl(strUrl);
wv.setWebViewClient(new WebViewClient());
wv.setWebChromeClient(new WebChromeClient());
wv.setPadding(0, 0, 0, 0);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);

Is this a known issue or if there's any solution to autoplay the video? Thanks!

解决方案

I think disabling autoplay is becoming a "standard".

By the way, you could try this

EDIT:

As the link is dead, I found it in https://lumi.do/p/04x626i1ngvht/how-to-autoplay-html5-video-on-android:

How to "autoplay" HTML5 video on Android
Written by Mathias Desloges in Labs on 19/05/11

Have you ever try to play with the new HTML5 video tag on an Android device?
We have and here is an stange issue which we faced.
Let's set up the context, we want our video starts playing just after the page load complete.
According to the HTML5 specification, we should use the "autoplay" attribute, but it has no effect in Android. So we tried with a little script tha call the play function after the page has loaded:

function callback () { document.querySelector('video').play(); } 
window.addEventListener("load", callback, false);

This reproduces well the behavior of the "autoplay" attribute on a classic desktop browser, but on an Android browser it fails.
we tried to attach the previously defined "callback()" function to a click event on a arbitrary node, and the video starts playing well "on click" (normal behavior). And while continue working on the page html code, we found the solution!

Don't ask me why, but after adding the "poster" attribute it works!

这篇关于YouTube iframe 嵌入无法在 Android 上自动播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆