如何在Android Studio的Web视图中启用YouTube视频下载?

时间:2019-01-03 15:04:07

标签: java android-studio webview youtube

我将YouTube网站视图添加到了使用Android Studio的Android应用程序中的新活动中。我的手机默认YouTube应用程序可以下载下图所示的视频。那么我可以在Android Studio中将下载工具启用到我的Web视图中吗?

请帮助我解决此问题。

enter image description here 这是我的网络视图

public class BeanVideos extends AppCompatActivity {
    WebView mWebView;
    ProgressBar progressBar;
    TextView textView;
    LinearLayout liProgressContainer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beanvideos);    
        textView = (TextView) findViewById(R.id.tvLoadingPercentage);
        liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        mWebView = (WebView) findViewById(R.id.webView);            
        progressBar.setVisibility(View.VISIBLE);
        mWebView.setWebViewClient(new Browser_home());
        mWebView.setWebChromeClient(new MyChrome());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        loadWebsite();
    }

    private void loadWebsite() {
        ConnectivityManager cm = (ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {                mWebView.loadUrl("https://www.youtube.com/channel/UC4UaWbUUUNqhb4GdsKLS6Q/videos");
        } else {
            mWebView.setVisibility(View.GONE);
        }
    }

    //back button function
    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    class Browser_home extends WebViewClient {
        Browser_home() {
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);
        }
    }

    private class MyChrome extends WebChromeClient {
        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        protected FrameLayout mFullscreenContainer;
        private int mOriginalOrientation;
        private int mOriginalSystemUiVisibility;
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
            textView.setText(progress + " %");
        }

        MyChrome() {}
        public Bitmap getDefaultVideoPoster()
        {
            if (mCustomView == null) {
                return null;
            }
            return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
        }
        public void onHideCustomView()
        {
         ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
            this.mCustomView = null;
     getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
            setRequestedOrientation(this.mOriginalOrientation);
            this.mCustomViewCallback.onCustomViewHidden();
            this.mCustomViewCallback = null;
        }

        public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
        {
            if (this.mCustomView != null)
            {
                onHideCustomView();
                return;
            }
            this.mCustomView = paramView;
            this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();    
            this.mOriginalOrientation = getRequestedOrientation();
            this.mCustomViewCallback = paramCustomViewCallback;
            ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
            getWindow().getDecorView().setSystemUiVisibility(3846);
        }
    }
}  

0 个答案:

没有答案