如何在webview android上显示弹出框架播放视频

时间:2013-02-24 23:12:49

标签: android android-layout android-webview

我希望在用户点击我的网页视图上的视频链接时显示弹出框,而不是弹出框应该显示,并进入该框架布局视频表面从SD卡播放我的视频,并在视频完成框架布局后自动关闭和网页 - 再次显示视图。 我搜索谷歌和stackoverflow有一些帮助,但我不明白有技术编写代码。在我的脑海中有一个混乱,我怎么能显示弹出框架布局以及如何显示webview。

目前我正在检查扩展程序,如果我发现视频扩展程序比我发送媒体播放器意图。但我不希望这样。我希望它应该在我的网络视图中播放,但在弹出式视频表面。

这是我的代码,我的布局文件中只包含webview,我从我的资产文件夹加载html,我正在播放SD卡中的视频..

感谢任何帮助

    setContentView(R.layout.activity_main);        
    WebView webView = (WebView) findViewById(R.id.webview);
    String html = "<embed src=\"file:///android_asset/" + "test.html" + " \"play=\"true\" loop=\"true\" width=\"100%\" height=\"100%\"> <embed>";
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    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(".m4v")) {
                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文件

<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/kummar.m4v" width="300"      onclick="playvideo('file:///sdcard/ku<p>testing of video element.</p>
  </video>
  </body>
  </html>

1 个答案:

答案 0 :(得分:0)

尝试此

的自定义对话框
    Dialog mVideoDialog ;
VideoView mVideoFullScreen;
 MediaController controller;

在你的增量方法中添加此

     mVideoDialog = new Dialog(this);
    mVideoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mVideoDialog.setContentView(R.layout.dialog);
    mVideoDialog.setOnKeyListener(this);
    mVideoFullScreen = (VideoView) mVideoDialog.findViewById(R.id.videoview1);
    controller = new MediaController(this);
    HelloWebViewClient webViewClient = new HelloWebViewClient();
    webView.setWebViewClient(webViewClient);
     }

 private class HelloWebViewClient extends WebViewClient
{ 
 @Override public boolean shouldOverrideUrlLoading(WebView view, String url)
 {

     if (url.contentEquals("file:///android_asset/01")) {
         showVideo(01); //check user click url from webview and pass int
     }
     else if (url.contentEquals("file:///android_asset/02"))
     {
        showVideo(02);//pass int to determine which video to play
     }
     else
     {
     System.out.println("DDD URL: "+url.toString()); view.loadUrl(url);  
     }
     return true;       
     }

这是showVideo方法

public void showVideo(int i) {
    // TODO Auto-generated method stub
    if(i==01)
    {
     mVideoDialog.show();
     mVideoFullScreen.setVideoPath("file:///sdcard/video file name.m4v");
     controller.setMediaPlayer(mVideoFullScreen);
     mVideoFullScreen.setMediaController(controller);
     mVideoFullScreen.requestFocus();
     mVideoFullScreen.start();
    }else
    {  } }