YouTube 视频未在 WebView 中播放 - Android [英] YouTube Video not playing in WebView - Android
问题描述
我想在 WebView 中播放 YouTube 视频,WebView 显示带有播放按钮的视频的第一眼,但在点击播放按钮后启动进度条,2-3 秒后停止进度条,屏幕为黑色.
I am tying to play YouTube video in WebView, WebView showing first look of video with play button, But after click on play button start progress bar and after 2-3 seconds stop progress bar and screen blank with black color.
Image1:带有播放按钮的视频先看
Image1: Video first look with play button
Image2:点击播放按钮后屏幕变为空白.
Image2: After click on play button screen goes blank.
拜托了!帮助我为什么视频无法启动.
Please! help me why video not starting.
IMAGE:1
图片:2
这是我在 webview 中播放 YouTubeVideo 的源代码.. 请帮帮我...
This is my source code to play YouTubeVideo in webview.. Please help me ...
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">
"
+ "</iframe>
";
return html;
}
推荐答案
在将 HTML 内容加载到 WebView 之前添加这些行.
Add these lines before loading HTML content to your WebView.
wv.setWebChromeClient(new WebChromeClient() {
});
来自文档:
为了在您的应用程序中支持内嵌 HTML5 视频,您需要打开硬件加速,并设置一个 WebChromeClient.对于全屏支持,需要实现 onShowCustomView(View, WebChromeClient.CustomViewCallback) 和 onHideCustomView(),getVideoLoadingProgressView() 是可选的.
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.
这篇关于YouTube 视频未在 WebView 中播放 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!