Unable to play video in html using webview from sdcard

a 夏天 提交于 2019-12-08 07:02:44

问题


I want to play video in webview from sdcard. I googled but could not get solution.I have tried this way:

public class WebActivity extends Activity {
private WebView mWebView;

@Override   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.helpview);
    mWebView =(WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setBuiltInZoomControls(true);

    String html ="<html><head></head><body>Playing Video<object width=\"550\" height=\"400\"> <param name=\"movie\" value=\"file://"+Environment.getExternalStorageDirectory()+"/test-embeded.swf\"> <embed src=\"file://"+Environment.getExternalStorageDirectory()+"/test-embeded.swf"+"\" width=\"550\" height=\"400\"> </embed> </object></body></html>";
    String mimeType = "text/html";
    String encoding = "utf-8";

    mWebView.loadDataWithBaseURL("null", html, mimeType, encoding, "");


  } 
 }

I got output as a blank screen.

I have even installed Adope Flash Player 10.1.apk but still the output is like that only

Could anybody know the solution?


回答1:


I have load a html page in a webview, which contain video Tag and i got the output. html file is in \assets folder, Here is my code it may help you,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webview);
    String html = "<embed src=\"file:///android_asset/" + "demo.html" + " \"play=\"true\" loop=\"true\" width=\"100%\" height=\"100%\"> <embed>";
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null);

    WebViewClient webViewClient = new WebViewClient();
    webView.setWebViewClient(webViewClient);

    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }


        @Override
        public void onLoadResource(WebView view, String url) {
              if (url.endsWith(".mp4")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse(url));
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);

            }
            else
            {
                super.onLoadResource(view, url);
            }
        }


    });
}

And my html page is,

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset=utf-8>
<title>Sample HTML5 Structure</title>
<SCRIPT LANGUAGE="JavaScript">
        function playvideo(url)
        {

                  window.location=url;

        }
</SCRIPT>
</head>
<body>
<video src="file:///sdcard/video.mp4" width="300" onclick="playvideo('file:///sdcard/video.mp4')" height="150" controls>
<p>If you are reading this, it is because your browser does not support the HTML5 video element.</p>
</video>
</body>
</html>



回答2:


A number of things could go wrong.

First, check in AndroidManifest.xml. Do you have included

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

?

Next, check for errors in the log (adb logcat) and post them here



来源:https://stackoverflow.com/questions/8293053/unable-to-play-video-in-html-using-webview-from-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!