以编程方式控制 WebView 中的 youtube 视频
Control a youtube video in WebView Programmatically
我正在 web 视图中加载 YouTube 视频,它工作正常,我想要的是能够以编程方式控制视频(即开始、停止等)。
我尝试了一些方法但没有用
String playVideo= "<html><body><iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/QKm-SOOMC4c?enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></body></html>";
myWebView = (WebView) findViewById( R.id.Webview );
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.loadData(playVideo, "text/html", "utf-8");
myWebView.loadUrl("javascript:playVideo()");
据我所知,开发者无法控制。
即使开发人员通过 getElementById 方法找到 html 元素并单击它来控制它,如果 Google 更改 html 结构,它也会被阻止。
推荐Google官方提供的方法。
https://developers.google.com/youtube/android/player/
你可以在这个库中看看我是怎么做的:android-youtube-player。
您可以先查看 WebView class。
调用webview.loadUrl("javascript:playVideo()");
的思路是正确的。但您需要等待 IFrame 库加载并准备就绪。
我正在 web 视图中加载 YouTube 视频,它工作正常,我想要的是能够以编程方式控制视频(即开始、停止等)。 我尝试了一些方法但没有用
String playVideo= "<html><body><iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/QKm-SOOMC4c?enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></body></html>";
myWebView = (WebView) findViewById( R.id.Webview );
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.loadData(playVideo, "text/html", "utf-8");
myWebView.loadUrl("javascript:playVideo()");
据我所知,开发者无法控制。
即使开发人员通过 getElementById 方法找到 html 元素并单击它来控制它,如果 Google 更改 html 结构,它也会被阻止。
推荐Google官方提供的方法。 https://developers.google.com/youtube/android/player/
你可以在这个库中看看我是怎么做的:android-youtube-player。
您可以先查看 WebView class。
调用webview.loadUrl("javascript:playVideo()");
的思路是正确的。但您需要等待 IFrame 库加载并准备就绪。