可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use android webview in my app for display video from youtube it's work well.
But i want it's auto play.
This is my activity i append "?autoplay=1" to videoUrl but nothing change it's still not auto play.
public class LandVideoAct extends Activity {
WebView mWebView;
String videoURL = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.landfull);
videoURL = "http://www.youtube.com/embed/VYIi49czywo?autoplay=1";
mWebView = (WebView) findViewById(R.id.fullwebview);
String vid = "<html><body style=\"margin: 0; padding: 0\"><iframe width=\"100%\" height=\"100%\" src=\""+videoURL+"\" type=\"text/html\" frameborder=\"0\"></iframe><body><html>";
WebChromeClient mWebChromeClient = new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
}
};
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setInitialScale(1);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT < 17) {
Log.i("GPSNETWORK", "<17");
} else {
Log.i("GPSNETWORK", Build.VERSION.SDK_INT+">=17");
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
mWebView.loadData(vid, "text/html", "UTF-8");
}
}
回答1:
I think its not possible in webview or android browsers. To achieve auto playing, I think you need "YOUTUBE API".
Check below link :
1] There's no way method to do autoplay video on android platform?
2] Youtube Api android autostart
These above links will give you idea about auto playing as well as youtube api.
回答2:
Check out this question. There are a couple of suggestions that may work for you, however the person who asked that question didn't say if it worked for them or not.
Most of what I'm reading is saying that most mobile platforms block autoplay to avoid poor user experience.
You can try this javascript code below:
var myvideo = document.getElementsByTagName('video')[0]; myvideo.play();
For Android 4.2.2+ try adding this in your native code:
WebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
Also check out this page. Which adds the code below to autoplay the video:
webview.setWebViewClient(new WebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://html5demos.com/video");
}
回答3:
Do like this. Hope it helps
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
+ "J2fB5XWj6IE?autoplay=1"
+ "&fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
From below link
In Chrome, autoplay is also disabled
(https://code.google.com/p/chromium/issues/detail?id=159336)
I believe the same happens with the recent versions of the default
Android Web Browser.
回答4:
It's disabled by default based on operating system reason. Chrome disables content by default, too. You can try to code your own app or use Java, Javascript and the YouTube-API instead. Maybe webkit will let you allow to decide what to play based on your battery state in the future.
回答5:
This code will do the trick, I'm using jquery, but it can be done with plain javascript.
<iframe id="myvideo" src="https://www.youtube-nocookie.com/embed/Vhh_GeBPOhs?autoplay=1&rel=0&controls=0&showinfo=0&rel=0&loop=1&modestbranding=1&wmode=transparent&mute=1amp;enablejsapi=1" width="560" height="315" frameborder="0" allowfullscreen></iframe>
<script>
setInterval(function(){
$('#myvideo')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
}, 250);
</script>
回答6:
Use Desktop mode on WebView. Youtube will autoplay. Add this:
webView.getSettings().setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Safari/537.36")
Like this:
val webView: WebView = findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.loadUrl("http://your.web.site/")
webView.webChromeClient = object : WebChromeClient() {}
webView.settings.setPluginState(WebSettings.PluginState.ON)
webView.settings.setPluginState(WebSettings.PluginState.ON_DEMAND)
webView.settings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Safari/537.36")
if(android.os.Build.VERSION.SDK_INT>16){webView.settings.setMediaPlaybackRequiresUserGesture(false)}
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(webView: WebView, url: String) {
super.onPageFinished(webView, url)
}
}
回答7:
A little late but I hope this helps.
After your page is finished loading run this:-
yourWebView.loadUrl("javascript:(function() { document.getElementsByClassName('ytp-large-play-button ytp-button')[0].click(); })()");