Android - WebView not playing YouTube videos

前端 未结 3 1960
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 03:11

I have some WebView code with which I am trying to play YouTube videos on a YouTube channel. But all it is doing is showing the spinner icon on a video and never actually st

相关标签:
3条回答
  • 2021-01-19 03:43

    Use

    <application
            android:hardwareAccelerated="true"
            android:allowBackup="true">
    </application>
    

    Perfectly working.

    0 讨论(0)
  • 2021-01-19 03:44

    Have you tried using your own WebChromeClient? This question seems relevant. WebView and HTML5 <video>

    You'll need to create one of these and implement it yourself, similar to what you're doing for the WebViewClient.

    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    VideoView video = (VideoView) frame.getFocusedChild();
                    frame.removeView(video);
                    a.setContentView(video);
                    video.setOnCompletionListener(this);
                    video.setOnErrorListener(this);
                    video.start();
                }
            }
        }
    });
    

    Make sure you add webview.getSettings().setPluginsEnabled(true); to your webview settings.

    And most importantly, for any webpage to load in a WebView, be sure to have the INTERNET permission in your Manifest:

    <uses-permission android:name="android.permission.INTERNET"/>
    
    0 讨论(0)
  • 2021-01-19 03:52

    Using WebView to play YouTube videos would require extensive testing and debugging on different Android OS versions due to the difference in functionality and bugs between Android 2.x and 4.x.

    A less bug-prone approach that gives you more control is to use YouTube Android Player API to embed a YouTube video into your own app, they have sample app so it shouldn't be too difficult if you follow their steps.

    0 讨论(0)
提交回复
热议问题