Mina Farid
Mina Farid

Reputation: 5887

Android: How to force WebView to play a video from youtube?

i want to play a youtube video using embeded url in webview in my app but the video is opened automatically in youtube application that is insatlled in my phone instead of played in webview in the activity

here is my code

    WebView mWebview = (WebView) findViewById(R.id.mwebview);
    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.getSettings().setBuiltInZoomControls(true);
    mWebview.getSettings().setSupportZoom(false);
    mWebview.loadUrl("http://www.youtube.com/embed/" + video.youtube_id);

Upvotes: 1

Views: 1704

Answers (1)

Mina Farid
Mina Farid

Reputation: 5887

i find the answer

    mWebview = (WebView) findViewById(R.id.mwebview);
    mWebview.setInitialScale(1);
    mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);

    mWebview.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });


    WebSettings webSettings = mWebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setAllowContentAccess(true);
    webSettings.setEnableSmoothTransition(true);
    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setSupportZoom(false);
    webSettings.setUseWideViewPort(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setSupportMultipleWindows(true);
    mWebview.loadUrl("http://www.youtube.com/embed/" + video.youtube_id);

Upvotes: 1

Related Questions