本文介绍了从我的网站转换网站到Android应用程序和Youtube视频不进入全屏模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android Studio中将网站转换为使用WebView的应用程序。一切都很好,但我网站上的youtube视频没有全屏显示。我是初学者,所以请帮我解决这个问题。

I converted a website into an app with WebView in Android Studio. Everything is fine but the youtube videos that was in my website does not go in full Screen. I am a beginner, so please help me in solving this.

     Main Activity Code  ---


import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import java.net.URI;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    //initializing WebView
    private WebView mwebView;
    private FrameLayout customViewContainer;
    private WebChromeClient.CustomViewCallback customViewCallback;
    private View mCustomView;
    private MyWebviewClient mWebChromeClient;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //WebView
        mwebView = (WebView) findViewById(R.id.myWebView);
        WebSettings webSettings = mwebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //improve webView performance
        mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mwebView.getSettings().setAppCacheEnabled(true);
        mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mwebView.setWebChromeClient(new  MyChromeBrowser());

        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);

        mWebChromeClient = new myWebChromeClient();
            webView.setWebChromeClient(mWebChromeClient);



        mwebView.loadUrl("https://laughonlyblog.wordpress.com");
        //force links open in webview only



    }

    private class MyChromeBrowser extends WebChromeClient {
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {

            super.onShowCustomView(view, callback);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        }


        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
        }
    }





    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
            mwebView.loadUrl("http://www.newsweek.com/us");
        } else if (id == R.id.nav_slideshow) {
            mwebView.loadUrl("http://www.newsweek.com/world");
        } else if (id == R.id.nav_manage) {
            mwebView.loadUrl("http://www.newsweek.com/tech-science");
        } else if (id == R.id.nav_gallery) {
            mwebView.loadUrl("http://www.newsweek.com/sports");
        } else if (id == R.id.nav_share) {
            mwebView.loadUrl("http://www.newsweek.com/about-newsweek");
        } else if (id == R.id.nav_send) {
            mwebView.loadUrl("http://www.newsweek.com/contact");
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private class MyWebviewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           /* if (Uri.parse(url).getHost().equals("www.laughonlyblog.wordpress.com/about/")) {
                //open url contents in webview
                return false;
            } else {
                //here open external links in external browser or app
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);




          } */
            view.loadUrl(url);
            return true;
        }

        //ProgressDialogue
        ProgressDialog pd = null;


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            pd=new ProgressDialog(MainActivity.this);
            pd.setTitle("Please Wait..");
            pd.setMessage("Website is Loading..");
            pd.show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
            super.onPageFinished(view, url);
        }
    }
    //goto previous page when pressing back button

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mwebView.canGoBack()) {
                        mwebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);


    }


}


推荐答案

要实现此目的,请执行以下步骤:

1)将WebChromeClient设置为您的webview。

Exmaple: WebView.setWebChromeClient(new MyChromeBrowser());


2)在WebChromeClient中实现onShowCustomView()和onHideCustomView()方法。

示例:`

To achieve this follows these steps :
1) Set WebChromeClient to your webview.
Exmaple : WebView.setWebChromeClient(new MyChromeBrowser());
2) Implement method onShowCustomView() and onHideCustomView() inside your WebChromeClient.
Example : `

private class MyChromeBrowser extends WebChromeClient {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
}`



3)给 android:hardwareAccelerated =true在您的活动的清单文件中。

示例:< activity android:name = .MainActivity
android:hardwareAccelerated =true
/>


更新:

要解决此问题,请尝试以下方法:

更改以下行: webView.setWebChromeClient(new MyWebChromeClient()); to


3) Give android:hardwareAccelerated="true" in manifest file for your activity.
Example : <activity android:name=".MainActivity" android:hardwareAccelerated="true" />
Update :
To fix this try these :
change these lines : webView.setWebChromeClient(new MyWebChromeClient()); to

mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);

在initWebView()方法中。

inside initWebView() method.

要让系统在全屏模式下确定最佳方向,请将此行添加到onShowCustomView(View视图,CustomViewCallback回调)方法:

To let the system decide the best orientation in full-screen mode add this line to onShowCustomView(View view, CustomViewCallback callback) method:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


更新:09-07-2017

或者你可以使用用于webview中的全屏视频播放器的VideoEnabledWebView库等等。

链接:

这篇关于从我的网站转换网站到Android应用程序和Youtube视频不进入全屏模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:16