aviciena
aviciena

Reputation: 71

How to playing full screen youtube video in Android webview?

I'm new in android developer. I have 2 questions :

  1. How to make full screen video immediately after tapping play sign?
  2. When the video in normal size and user want to scroll the page which is having header and in static mode, the video will cover the header. It should be below the header when user scroll it until the header. How to make the video below the header when user scroll the page until header position?

This is my Code :

        String widthAndHeight = "width='220' height='200'";
        String videoURL = "http://www.youtube.com/v/AyeJyctGhSc&feature=youtube_gdata";

        String temp = "<object "+widthAndHeight+">" +
        "<param name='allowFullScreen' value='false'>" +
        "</param><param name='allowscriptaccess' value='always'>" +
        "</param><embed src='"+ videoURL +"'" +
        " type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true'" + widthAndHeight +
        "></embed></object>";

        video.getSettings().setPluginState(PluginState.ON);
        video.getSettings().setJavaScriptEnabled(true);
        video.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        video.getSettings().setPluginsEnabled(true);
        video.getSettings().setSupportMultipleWindows(false);
        video.getSettings().setSupportZoom(false);
        video.setVerticalScrollBarEnabled(false);
        video.setHorizontalScrollBarEnabled(false);
        video.loadData(temp,"text/html", "utf-8");

Upvotes: 4

Views: 11422

Answers (3)

BaDo
BaDo

Reputation: 578

Use this source to play Youtube Video

        String video = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 100%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
                + youtubeId +
                "?autoplay=1"
                + "&fs=0\" frameborder=\"0\">\n"
                + "</iframe>\n";
        mWebview.getSettings().setPluginState(PluginState.ON);
        mWebview.setWebChromeClient(new WebChromeClient());
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.setHorizontalScrollBarEnabled(false);
        mWebview.setVerticalScrollBarEnabled(false);
        mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebview.getSettings().setBuiltInZoomControls(false);
        mWebview.getSettings().setAppCacheEnabled(true);
        mWebview.setInitialScale(0);
        mWebview.getSettings().setLoadWithOverviewMode(true);
        mWebview.getSettings().setUseWideViewPort(true);
        mWebview.loadData(video,"text/html","UTF-8");

Upvotes: 2

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Add below code in your Activity :

WebView.setWebChromeClient(new WebChromeClient()

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            customComponenet.addView(view);
            mWebView.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onHideCustomView() {
            if (customComponenet == null)
                return;
            // Hide the custom view.
            customComponenet.setVisibility(View.GONE);

            mWebView.setVisibility(View.VISIBLE);
        }
    });

where customComponent is your FrameLayout.

Upvotes: 1

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

To play a youtube video

you have to parse the url and play the video in videoview

Upvotes: 0

Related Questions