1

我正在尝试从 url 在 webview 上显示 youtube 视频。一个小时后它的工作,但仍然没有显示任何东西: -

图片

在此处输入图像描述

正如你在上面看到的图像没有显示任何东西,但寻找视频条增加(但没有显示任何东西,我使用API 17 和 480*800(WVGA)),下面是我的代码: -

代码

movie_image_movie_link.setOnClickListener(new OnClickListener() {

            @SuppressLint("SetJavaScriptEnabled")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                final Dialog dialog = new Dialog(act);
//              dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.show_video_from_youtube);
                dialog.setTitle("YouTube Video");
                WebView video = (WebView) dialog.findViewById(R.id.webview);


                video.getSettings().setJavaScriptEnabled(true);
                video.getSettings().setPluginState(WebSettings.PluginState.ON);
                // video.getSettings().setUserAgent(0);
                video.setWebChromeClient(new WebChromeClient() {
                });

                String id = StringUtils.substringBetween(
                        MovieJSONObjectHandle.youtube_link,
                        "www.youtube.com/watch?v=", "&amp");

                System.out.println("url => "
                        + Uri.parse("http://www.youtube.com/v/" + id)
                                .toString());

                final String mimeType = "text/html";
                final String encoding = "UTF-8";
                String html = getHTML(id);
                video.loadDataWithBaseURL("", html, mimeType,
                        encoding, "");

                dialog.show();

            }
        });


public static String getHTML(String videoId) {

        String html = "<iframe class=\"youtube-player\" "
                + "style=\"border: 0; width: 100%; height: 90%;"
                + "padding:0px; margin:0namepx\" "
                + "id=\"ytplayer\" type=\"text/html\" "
                + "src=\"http://www.youtube.com/embed/" + videoId
                + "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
                + "controls onclick=\"this.play()\">\n" + "</iframe>\n";

        /**
         * <iframe id="ytplayer" type="text/html" width="640" height="360"
         * src="https://www.youtube.com/embed/WM5HccvYYQg" frameborder="0"
         * allowfullscreen>
         **/

        return html;
    }

也尝试使用视频而不是 iframe 但仍然存在同样的问题:-

String html = "<video id=\"video\" width=\"320\" height=\"240\" src=\"http://www.youtube.com/embed/" + videoId
+ "autobuffer controls onclick=\"this.play();\">";

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="400dp"
    android:gravity="center"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>

主要节日

<activity
    android:name="biz.xicom.defindme.controlpage.ControlPage"
    android:hardwareAccelerated="true"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Light.NoTitleBar" >
</activity>

所以如果你过去遇到过这个问题,那么请告诉我我在上面的代码中犯了什么错误。

4

1 回答 1

0

尝试这个:

    webView = (WebView) findViewById(R.id.webView);

    mWebViewClient = new myWebViewClient();
    webView.setWebViewClient(mWebViewClient);

    mWebChromeClient = new myWebChromeClient();
    webView.setWebChromeClient(mWebChromeClient);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSaveFormData(true);
   webView.loadUrl("http://m.youtube.com");  // your web url
于 2013-06-25T12:42:21.973 回答