如何在android webview中播放视频URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20322057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play video URL inside android webview
提问by Alok Tiwari
I want to play video url inside my application webview but when i am run the application it showing only white screen . i had read some post on this and i have used that code but video is not playing in webview it launch device player but my requirement not this .
我想在我的应用程序 webview 中播放视频 url,但是当我运行该应用程序时,它只显示白屏。我已经阅读了一些关于此的帖子,我已经使用了该代码,但是视频没有在 webview 中播放,它启动设备播放器,但我的要求不是这个。
please help me if some one already done this . my code is here :-
如果有人已经这样做了,请帮助我。我的代码在这里:-
WebView webView = (WebView) findViewById(R.id.myweb);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
setProgressBarIndeterminateVisibility(false);
super.onPageFinished(view, url);
}
});
webView.loadUrl("http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4");
webView.setVisibility(View.VISIBLE);
Testing URL is working in browser .
测试 URL 在浏览器中工作。
回答by wadali
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("https://www.youtube.com");
also set in manifist file
也在清单文件中设置
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
回答by Siddharth_Vyas
Add webview.setWebChromeClient(new WebChromeClient());
and to enable plugins for your video add:
添加webview.setWebChromeClient(new WebChromeClient());
并为您的视频启用插件添加:
if (Build.VERSION.SDK_INT < 8) {
webview.getSettings().setPluginsEnabled(true);
}
else {
webview.getSettings().setPluginState(PluginState.ON);
}
Hope this helps.
希望这可以帮助。
回答by ksasq
You need to load an HTML document that embeds the video inside of it, instead of the video directly. To play inline video in WebView using the <video>
tag you must also ensure the hardware acceleration is enabled in your application (see http://developer.android.com/guide/topics/graphics/hardware-accel.html).
您需要加载一个将视频嵌入其中的 HTML 文档,而不是直接加载视频。要使用<video>
标签在 WebView 中播放内嵌视频,您还必须确保在您的应用程序中启用了硬件加速(请参阅http://developer.android.com/guide/topics/graphics/hardware-accel.html)。
回答by Ruben Flores
I know this is an old issue, I had the same problem some days back and surfing across internet I found this complete source that helped me a lot, and now i want to share with others, maybe can help others like me..
我知道这是一个老问题,几天前我遇到了同样的问题,我在网上冲浪时找到了这个对我有很大帮助的完整资源,现在我想与他人分享,也许可以帮助像我这样的人..
回答by Kevin Wu
If tried others anwsers no work,maybe you can try this:
如果试过其他人没有工作,也许你可以试试这个:
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
回答by Jupiter
This may be an old question but this works:
这可能是一个老问题,但这有效:
//Here Im loading the html file from disk
String html_file = LoadData(file.getAbsolutePath());
WebView myWebView = (WebView) view.findViewById(R.id.webView);
myWebView.reload();
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(html_file, "text/html; charset="utf-8", "UTF-8");
Use base64 encoded video data as your video
source:
使用 base64 编码的视频数据作为video
源:
//html_file
<video controls>
<source type="video/mp4" src="data:video/webm;base64,MY_BASE64_ENCODED VIDEO_HERE">
</video>
回答by Erik ?sterlund
For me this was the solution
对我来说,这是解决方案
settings.setDomStorageEnabled(true);
回答by user7856586
I also got stuck with this issue. I got correct response from server, but couldn`t play video. After long time I found a solution here. Maybe, in future this link will be invalid. So, here is my correct code
我也被这个问题困住了。我得到了服务器的正确响应,但无法播放视频。很长一段时间后,我在这里找到了解决方案。也许,将来此链接将无效。所以,这是我的正确代码
Uri video = Uri.parse("Your link should be in this place ");
mVideoView.setVideoURI(video);
mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// here write another part of code, which provides starting the video
}}
回答by Darshan Singh
Try this.
尝试这个。
{
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setMinimumFontSize(10);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
webView.setVerticalScrollBarEnabled(false);
webView.loadDataWithBaseURL(null, vUrl, "text/html", "UTF-8", null);
}
回答by Saunik Singh
No you can't play the video by link in this case. To do this you should create an RTSPconnection then you can play the video through an RTSP url in Webview
. You can retrieve an RTSP connection from a server like Youtube.
不,在这种情况下,您无法通过链接播放视频。为此,您应该创建一个RTSP连接,然后您可以通过Webview
. 您可以从 Youtube 等服务器检索 RTSP 连接。