Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: playing youtube video on Webview

i've a problem to playing youtube video on a WebView. It's all day i reading question and answer on how to do this, but it doesn't work. I've already set the Manifest and this is the code i'm using

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


    mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebview.setWebChromeClient(new WebChromeClient());
    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.getSettings().setAppCacheEnabled(true);
    mWebview.setInitialScale(1);
    mWebview.getSettings().setLoadWithOverviewMode(true);
    mWebview.getSettings().setUseWideViewPort(true);

    //webSettings.setLoadsImagesAutomatically(true);
    //webSettings.setSupportZoom(false);

    mWebview.loadUrl("https://www.youtube.com/embed/MYVIDEOID");

and what's happen is this i can click the play button the timer start but i can see nothing.

EDIT: The problem seems on the emulator and not on real device, check the comment for the answer.

like image 304
justo Avatar asked Jul 18 '17 12:07

justo


People also ask

Can I download a page from YouTube in a WebView?

But you can’t just download a page from Youtube in your WebView, so your application will only look like a web browser with a poor user experience. The best way to play Youtube Videos on the web is to use the IFrame Player API from Youtube. This is a JavaScript API that provides access to a web-based Youtube player.

What is Android-YouTube-player?

This library is called android-youtube-player , it is an API to play Youtube Videos in an Android WebView, with a way to feel natural and take the least effort for developers. You do not have to write any web pages or JavaScript at all. I. Why should you consider not using the official library from YouTube?

How do I play a YouTube video on a web page?

The best way to play Youtube Videos on the web is to use the IFrame Player API from Youtube. This is a JavaScript API that provides access to a web-based Youtube player. To use it in your application, you need to write a web page and then upload that page to a WebView.

Why won't the video play in the web view?

If you directly try to load the URL in the web view and run it then the video won't play. One difficult way of solving this problem is to stream the video in the video view.


1 Answers

try this

String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";

    WebView displayYoutubeVideo = (WebView) findViewById(R.id.mWebView);
    displayYoutubeVideo.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    WebSettings webSettings = displayYoutubeVideo.getSettings();
    webSettings.setJavaScriptEnabled(true);
    displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");

also set in manifist file android:hardwareAccelerated="true"

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

</application>
like image 107
AskNilesh Avatar answered Oct 24 '22 09:10

AskNilesh