gpt4 book ai didi

android - YouTube 视频未在 WebView 中播放 - Android

转载 作者:IT王子 更新时间:2023-10-28 23:51:34 24 4
gpt4 key购买 nike

我想在 WebView 中播放 YouTube 视频,WebView 显示带有播放按钮的视频的第一个外观,但是在单击播放按钮后启动进度条并在 2-3 秒后停止进度条并且屏幕为黑色。

Image1:带播放按钮的视频优先观看

Image2:点击播放按钮后屏幕变为空白。

请!帮助我为什么视频没有开始。

图像:1 This is first look of web view

图像:2 enter image description here

这是我在 webview 中播放 YouTubeVideo 的源代码。请帮助我...

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}

public String getHTML() {
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"
+ "?fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
return html;
}

最佳答案

在将 HTML 内容加载到 WebView 之前添加这些行。

wv.setWebChromeClient(new WebChromeClient() {
});

来自文档:

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient. For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.

关于android - YouTube 视频未在 WebView 中播放 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com