How can we play YouTube embeded code in an Android application using webview?

33,235

Solution 1

Check out the following SO post: How to embed a YouTube clip in a WebView on Android

Try the following code which may help you. I just framed it for your reference

myWebView = (WebView) findViewById( R.id.webview_compontent );

String playVideo= "<html><body>Youtube video .. <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"

myWebView.loadData(playVideo, "text/html", "utf-8");

Refer the following link for how to use Webview: http://developer.android.com/reference/android/webkit/WebView.html

Solution 2

Add WebView in XML File

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

And in Java File

String frameVideo = "<html><body>Youtube video .. <br> <iframe width="320" height="315" src="https://www.youtube.com/embed/lY2H2ZP56K4" frameborder="0" allowfullscreen></iframe></body></html>";

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

displayVideo.loadData(frameVideo, "text/html", "utf-8");

For Tutorial:

  1. How to embed youtube video in android

  2. Android WebVew tutorial with example

Solution 3

Hi if you get a blackscreen maybe make sure that you enable plugins

WebView engine = (WebView) findViewById(R.id.webview);
engine.getSettings().setJavaScriptEnabled(true);
engine.getSettings().setPluginsEnabled(true);
engine.loadUrl("http://www.youtube.com/embed/bIPcobKMB94?autoplay=1");

Solution 4

public class webview_youtube extends Activity{

    String video_id = "";
    ImageView back_btn;
    ProgressBar progressBar;
    html = "";
    WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.video_screen);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);



            video_id = "KK9bwTlAvgo";


        initializeViews();
    }

    public void initializeViews() {
        try {

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

            if (video_id.equalsIgnoreCase("")) {
                finish();
                return;
            }

            WebSettings ws = webView.getSettings();
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            ws.setPluginState(WebSettings.PluginState.ON);
            ws.setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.reload();


                html = getHTML(video_id);

            webView.loadData(html, "text/html", "UTF-8");

            WebClientClass webViewClient = new WebClientClass(progressBar);
            webView.setWebViewClient(webViewClient);
            WebChromeClient webChromeClient = new WebChromeClient();
            webView.setWebChromeClient(webChromeClient);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            webView.loadData("", "text/html", "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        try {
            webView.loadData("", "text/html", "UTF-8");
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class WebClientClass extends WebViewClient {
        ProgressBar ProgressBar = null;

        WebClientClass(ProgressBar progressBar) {
            ProgressBar = progressBar;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            ProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            ProgressBar.setVisibility(View.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i("webview-click :", "" + url.toString());

            //her the youtube url gets loaded to show video
            view.loadUrl(getHTML(video_id));
            return true;
        }
    }

    public String getHTML(String videoId) {
        String html = "<iframe class=\"youtube-player\" " + "style=\"border: 0; width: 100%; height: 96%;"
                + "padding:0px; margin:0px\" " + "id=\"ytplayer\" type=\"text/html\" "
                + "src=\"http://www.youtube.com/embed/" + videoId
                + "?&theme=dark&autohide=2&modestbranding=1&showinfo=0&autoplay=1\fs=0\" frameborder=\"0\" "
                + "allowfullscreen autobuffer " + "controls onclick=\"this.play()\">\n" + "</iframe>\n";
        Log.i("video-id from html url= ", "" + html);
        return html;
    }

}
Share:
33,235
Droid
Author by

Droid

Updated on July 05, 2022

Comments

  • Droid
    Droid almost 2 years

    How can we play YouTube embeded code in an Android application using webview?

  • Droid
    Droid about 13 years
    thanx .i have same code as this link ..<object.... but i dont get any idea how to play this ? if u have than plz help me .
  • Droid
    Droid about 13 years
    thanx vinayak. i get black screen but video not play yet in my Galaxy Tab .
  • Oh Danny Boy
    Oh Danny Boy over 12 years
    This opens the URL in an external web browser in 2.2.
  • IronBlossom
    IronBlossom about 12 years
    @Oh Danny Boy implement your own WebViewClient and Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; }
  • touti
    touti about 11 years
    thanks it work , but i still have a problem with full screen mode
  • hitesh141
    hitesh141 over 7 years
    how to make java script enabled in browser in android? can you please help me out?
  • Morton
    Morton over 7 years
    you can use webVIEW.getSettings().setJavaScriptEnabled(true); before loadData , hope it's not too late :)
  • gumuruh
    gumuruh about 4 years
    yes this solved my problem too. But i wonder how can we count the time elapsed while playing the video on android ? @Vinayak
  • Matan Dahan
    Matan Dahan over 3 years
    This works great! Please change http to https for it to work properly
  • Shrikant
    Shrikant almost 3 years
    10 years later, this solution not working