Android Studio WebView Youtube Video BlackScreen

Deadly 提交于 2020-01-07 03:15:48

问题


I want to play a video in the WebView but somehow I get an blackscreen on the video but I can hear the sound of it. Would be really glad if someone can help me

public class MainActivity extends AppCompatActivity {

WebView displayYoutubeVideo;
TextView button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String frameVideo = "<html><body>Video From YouTube<br><iframe 
    width=\"560\" height=\"315\" 
    src=\"https://www.youtube.com/embed/WtO3_tF8niU\" frameborder=\"0\" 
    allowfullscreen></iframe></body></html>";

    WebView displayYoutubeVideo = (WebView) findViewById(R.id.youtube_view);

    displayYoutubeVideo.getSettings().setUserAgentString("Mozilla/5.0 
           (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");

    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");
}
}

回答1:


I guess you might be missing android:hardwareAccelerated="true" in the permissions of your activity in the manifest file. Since you are playing a flash video this should work out to make the WebView flash enabled. This is something that I encountered and the resolution was to add this permission.




回答2:


This link may help you.It seems you missed some properties for the web view.

https://inducesmile.com/android/embed-and-play-youtube-video-in-android-webview/




回答3:


You must activate Hardware acceleration if your android:targetSdkVersion is lower than 11 and have to set webChromeclient to your webview

displayYoutubeVideo.setWebChromeClient(new WebChromeClient());

and in your manifest file add below line

 <application android:hardwareAccelerated="true" ...>


来源:https://stackoverflow.com/questions/44403872/android-studio-webview-youtube-video-blackscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!