在webview中显示youtube视频,显示空白页面;单击播放按钮时,屏幕将变为空白

时间:2014-01-22 07:01:36

标签: java android webview youtube

我从我的服务器获取youtube id并且我在youtube中嵌入了url url,之后我在listview中显示youtube url,同时点击listitem youtube video必须在webview中播放,这是我的要求,我尝试了所有不适合我的方式,它显示空白页

像这样我从我的服务器获取并制作为youtube url

app1.setUrlWiki("http://www.youtube.com/embed/"+json.getString("youtube_url")+"?fs=0");

这是我的mainactivity.java

lv2 =(ListView)findViewById(R.id.listV_main);   
        lv2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv2.getItemAtPosition(position);
                Application1 obj_itemDetails = (Application1)o;


                Intent intent1=new Intent(PoojaVideos.this,WebViewActivity.class);
                //Log.d("test","strContactList: "+strContactList);
                //intent1.putExtra("firstKeyName", Uri.parse(((Application1) o).getUrlWiki()));
                String link = Uri.parse(((Application1) o).getUrlWiki()).toString();
                intent1.putExtra("firstKeyName", link);
                startActivity(intent1);


            } 
        });

从那里我根据点击

将网址发送到webview活动

webviewactivity.java  音频单独来了,但视频没有显示

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);
             //wv = (WebView) findViewById(R.id.webView);

              mContentView = (FrameLayout) findViewById(R.id.main_content);
             wv = (WebView) findViewById(R.id.webView);
             mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);

            wv.getSettings().setJavaScriptEnabled(true);
            wv.getSettings().setPluginsEnabled(true);
            wv.setWebChromeClient(new WebChromeClient() {
            });
            final String mimeType = "text/html";
            final String encoding = "UTF-8";
            String html = getHTML();
            System.out.println(".................."+html);
            wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

        }

        public String getHTML() {

            Intent intent1= getIntent(); // gets the previously created intent
        String firstKeyName1 = intent1.getStringExtra("firstKeyName");
             Toast.makeText(this, firstKeyName1, 1000).show();
             System.out.println("kkkkk."+firstKeyName1);
             String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\"  src="firstKeyName1" frameborder=\"0\">\n"
                        + "</iframe>\n";

                return html;

        }

webview.xml

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

点击播放按钮,音频正在运行,但视频未显示,显示黑屏。**

**According to the answer i changed my code,now its working fine;i checked in device its working fine but in emulator audio only coming.so guys check in device**

2 个答案:

答案 0 :(得分:1)

您必须以这种方式在html字符串中设置网址:

String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src='"+firstKeyName1 +"' frameborder=\"0\">\n"
                        + "</iframe>\n";

现在,您可以在该屏幕中轻松查看YouTube视频。

答案 1 :(得分:1)

根据文件

  

为了在您的应用程序中支持内联HTML5视频,您需要   打开硬件加速,并设置WebChromeClient。   对于全屏支持,onShowCustomView(View,   WebChromeClient.CustomViewCallback)和onHideCustomView()都是   必需的,getVideoLoadingProgressView()是可选的。

android:hardwareAccelerated="true"放入AndroidManifest。