在Webview中播放视频(嵌入)

时间:2015-10-23 07:59:17

标签: android video webview

我正在尝试在webview内播放视频。我有视频嵌入网址。视频嵌入可以来自不同的来源。我检查了很多建议,但没有一个对我有用。我甚至尝试将整个<iframe>传递给webview。但它仍然无法正常工作。但是当我设置整个webview网址时,它会加载。为什么它不使用视频嵌入网址。以下是我的代码:

public void showEmbedinWebview(String embedUrl) {
        if (getActivity() != null && embedUrl != null) {
            FrameLayout videoContainer = new FrameLayout(getActivity());
            videoContainer.setLayoutParams(UiUtils.getLayoutParams());
            setContainerId(videoContainer);
            mTextviewContainer.addView(videoContainer);
            VideoEnabledWebView vWebview = new VideoEnabledWebView(getActivity());
            vWebview.setLayoutParams(UiUtils.getLayoutParams());
            setWebViewId(vWebview);
            vWebview.getSettings().setJavaScriptEnabled(true);
            vWebview.getSettings().setDomStorageEnabled(true);
            vWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

            vWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
            vWebview.setBackgroundColor(Color.parseColor("#ffffff"));
            android.webkit.CookieManager.getInstance().setAcceptCookie(true);
            videoContainer.addView(vWebview);
            vWebview.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    showProgressBar();

                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    hideProgressBar();
                    super.onPageFinished(view, url);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(mActivity, mActivity.getResources().getString(R.string.no_network_msg), Toast.LENGTH_SHORT).show();
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    return false;
                }
            });

            if (embedUrl != null) {
                vWebview.loadUrl(embedUrl);
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

而不是在webview中播放视频,而是使用VideoView。

这是在videoview中播放视频的代码。

            videoview video=(VideoView)findViewById(R.id.videoview);

            video.setVisibility(View.VISIBLE);
            MediaController mc = new MediaController(this);
            mc.setAnchorView(video);
            mc.setMediaPlayer(video);
            url = url.replace(" ", "%20");
            Log.e("url",url);
            Uri videoLInk = Uri.parse(url);
            video.setMediaController(mc);
            video.setVideoURI(videoLInk);
            video.requestFocus();
            video.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer arg0) {
                    pDialog.dismiss();
                    video.start();
                }
            });

希望它会对你有所帮助。