Facebook视频无法在Android WebView中播放

时间:2018-03-26 09:41:04

标签: android facebook webview

我创建了一个Android应用程序,它有一个加载https://www.facebook.com的webview。但是,当我在手机中运行此应用程序时,Facebook中的视频无法正常播放。仅播放视频的声音,但视频已冻结。我认为webview的某些属性没有正确设置。我发布了一段代码。代码什么错了?

public class FacebookFragment extends Fragment implements{

private FacebookVideoUrlManager facebookVideoUrlManager;
//webview 
private WebView facebookWebView;

//progress bar
private ProgressBar webProgressBar;

private String url = "https://facebook.com";

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message message) {
        switch (message.what) {
            case 1:{
                webViewGoBack();
            }break;
        }
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_facebook, container, false);

    webProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    facebookVideoUrlManager = new FacebookVideoUrlManager();

    facebookWebView = (WebView) view.findViewById(R.id.facebook_webview);
    WebSettings settings = facebookWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(false);
    facebookWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    facebookWebView.getSettings().setBuiltInZoomControls(false);
    //facebookWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    //facebookWebView.loadUrl(url);

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

    facebookWebView.setOnKeyListener(new View.OnKeyListener(){

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            facebookVideoUrlManager.setUrl("");

            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.getAction() == MotionEvent.ACTION_UP
                    && facebookWebView.canGoBack()) {
                handler.sendEmptyMessage(1);

                return true;
            }
            else if (!facebookWebView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){
                exitConfirmationDialog();
                return true;
            }
            return false;
        }

    });

    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

}

private void showFacebookOnBrowser() {
    facebookWebView.getSettings().setJavaScriptEnabled(true);
    facebookWebView.getSettings().getLoadWithOverviewMode();
    facebookWebView.getSettings().getUseWideViewPort();
    facebookWebView.getSettings().getAllowContentAccess();
    facebookWebView.getSettings().getAllowFileAccessFromFileURLs();
    facebookWebView.getSettings().getAllowFileAccess();
    facebookWebView.getSettings().getAllowUniversalAccessFromFileURLs();
    facebookWebView.getSettings().getCacheMode();
    facebookWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    facebookWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webProgressBar.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            webProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadResource(WebView view, String url) {


        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return false;
        }
    });
    facebookWebView.loadUrl("https://www.facebook.com");


}

private void webViewGoBack(){
    facebookWebView.goBack();
}}

0 个答案:

没有答案