无法使用 sdcard 中的 webview 播放 html 视频

发布于 2024-12-18 11:16:21 字数 1276 浏览 0 评论 0原文

我想从 sdcard 在 webview 中播放视频。我用谷歌搜索但无法找到解决方案。我尝试过这种方式:

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, "");


  } 
 }

我得到的输出为空白屏幕。

我什至安装了 Adope Flash Player 10.1.apk 但输出仍然是这样,

有人知道解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-12-25 11:16:22

我已经在 webview 中加载了一个 html 页面,其中包含视频标签,并且我得到了输出。 html 文件位于 \assets 文件夹中,这是我的代码,它可能对您有帮助,

@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);
            }
        }


    });
}

我的 html 页面是,

<!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>

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>
独闯女儿国 2024-12-25 11:16:22

很多事情都可能出错。

首先,检查AndroidManifest.xml。你有包括在内吗

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

接下来,检查日志(adb logcat)中的错误并将其发布到此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文