Android WebView with an embedded youtube video, full screen button freezes video

后端 未结 3 1160
陌清茗
陌清茗 2020-11-29 04:54

I have an android webview that loads a wordpress blog. Some blog posts contain youtube videos which I would like the user to be able to make full screen if they wish. The pr

3条回答
  •  我在风中等你
    2020-11-29 05:20

    You can start an external YouTube app when you will cath video info URLif it is not important to show YouTube video directly in application.

    To catch video info URL You need to owerride onLoadResource method:

    new WebViewClient() {
    
        @Override
        public void onLoadResource(WebView view, String url) {
    
            if (url.startsWith("http://www.youtube.com/get_video_info?")) {
                try {
                    String path = url.replace("http://www.youtube.com/get_video_info?", "");
    
                    String[] parqamValuePairs = path.split("&");
    
                    String videoId = null;
    
                    for (String pair : parqamValuePairs) {
                        if (pair.startsWith("video_id")) {
                            videoId = pair.split("=")[1];
                            break;
                        }
                    }
    
                    if(videoId != null){
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"))
                                .setData(Uri.parse("http://www.youtube.com/watch?v=" + videoId)));
                        needRefresh = true;
    
                        return;
                    }
                } catch (Exception ex) {
                }
            } else {
                super.onLoadResource(view, url);
            }
        }
    }
    

提交回复
热议问题