WebView 加载网页内部video 横向全屏播放问题

            WebView 加载网页内部video 横向全屏播放问题
目的需要加载一个文本型的html 并控制video未横向的问题。 由

这里写图片描述
变成这样的
这里写图片描述

html是从json中获取的 是这样的

private String mStrHtml = "<h1 style=\"margin: 5px auto 10px; padding: 0px; font-stretch: normal; font-size: 38px; line-height: 57px; font-family: 微软雅黑; clear: both; color: rgb(34, 34, 34); white-space: normal;\"><span style=\"font-size: 24px;\">苏州四县市三区创建首批省级食品安全城市</span></h1><p>2017-04-05 15:26:05</p><hr/><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">昨天上午,我市召开苏州市食安委全体(扩大)会议暨创建省级食品安全城市推进工作会议,总结上半年全市食品安全工作,研究部署下半年食品安全工作任务及省级食品安全城市创建工作。市委副书记、副市长周伟强出席会议。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  市食安办通报了上半年我市食品安全工作情况。今年以来,我市深入贯彻习近平总书记提出的食品安全“四个最严”的要求,市食安办积极发挥牵头和综合协调作用,统一部署全市食品安全工作,各地各部门加强食品安全监管,深入开展食品安全“放心行动”专项整治,坚决严惩违法行为。今年的“放心行动”重点开展食用农产品、桶装饮用水、食用油、黄酒、学校食堂及学校周边餐饮、旅游景区餐饮、“两超一非”、网络订餐、农村食品市场等9项专项整治行动。  上半年全市累计出动监管人员90370人次,检查各类食品生产经营单位95813家次,查处食品案件798起,罚没款831.8万元,销毁不合格食品180吨。加强行政执法与刑事司法衔接,强化刑事责任追究,全市移送公安案件41件,采取强制措施58人。针对新兴的网络平台订餐情况,市食药监部门积极主动介入,开展网络订餐状况调研,约谈饿了么、百度外卖、美团外卖、零号线等5家主要第三方平台,签订《互联网订餐食品安全承诺书》,要求其把好餐饮单位上线准入关,严格履行食品安全主体责任。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  周伟强指出,各地政府和食品安全监管部门要准确把握食品安全工作的新要求,用新的发展理念指导食品安全工作,以社会共治的思路推进食品安全工作,继续推动落实“四有两责”,着力推进长效机制建设,严厉打击食品安全违法行为,强化监管和检测能力建设,进一步完善督查考评机制,切实保障人民群众“舌尖上的安全”。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\"><img src=\"http://mdm.ahd6g.cn/uploads/image/20170405/1491377317568195.jpg\" title=\"1491377317568195.jpg\" alt=\"timg (5).jpg\" width=\"100%\"/></p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  昆山、张家港、常熟、太仓四市及吴江区、相城区、苏州高新区三区正在参与江苏省首批省级食品安全城市创建,动员部署、组织创建阶段现已基本完成,市食安委将加强跟踪督导,组织相关人员按照创建标准开展督查,对发现的问题及时督促整改,迎接下一阶段的考核评价。周伟强要求,各地要用好“食品安全城市创建”这一有力抓手,充分发挥政府主导作用,调动各部门力量和资源,激发人民群众和社会各界参与创建的积极性、主动性,形成食品安全社会共治局面。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  副市长盛蕾出席会议。</p><p style=\"text-align: center;\"><video class=\"edui-upload-video vjs-default-skin video-js\" controls=\"\" preload=\"none\" poster=\"http://mdm.ahd6g.cn/uploads/images/20170407/14915313834964.jpg\" width=\"100%\" src=\"http://mdm.ahd6g.cn/uploads/videos/20170405/1491381800143098.mp4\" data-setup=\"{}\"><source src=\"http://mdm.ahd6g.cn/uploads/videos/20170405/1491381800143098.mp4\" type=\"video/mp4\"/></video></p>";

第一步 布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.baixi.webviewhtmlvideo.MainActivity"
    android:orientation="horizontal">
    <FrameLayout
        android:id="@+id/fl_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
    <WebView
        android:id="@+id/webview"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        />
</LinearLayout>

第二部 设置web

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    private FrameLayout mLayout;
    private View nVideoView = null;
    private String mStrHtml = "<h1 style=\"margin: 5px auto 10px; padding: 0px; font-stretch: normal; font-size: 38px; line-height: 57px; font-family: 微软雅黑; clear: both; color: rgb(34, 34, 34); white-space: normal;\"><span style=\"font-size: 24px;\">苏州四县市三区创建首批省级食品安全城市</span></h1><p>2017-04-05 15:26:05</p><hr/><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">昨天上午,我市召开苏州市食安委全体(扩大)会议暨创建省级食品安全城市推进工作会议,总结上半年全市食品安全工作,研究部署下半年食品安全工作任务及省级食品安全城市创建工作。市委副书记、副市长周伟强出席会议。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  市食安办通报了上半年我市食品安全工作情况。今年以来,我市深入贯彻习近平总书记提出的食品安全“四个最严”的要求,市食安办积极发挥牵头和综合协调作用,统一部署全市食品安全工作,各地各部门加强食品安全监管,深入开展食品安全“放心行动”专项整治,坚决严惩违法行为。今年的“放心行动”重点开展食用农产品、桶装饮用水、食用油、黄酒、学校食堂及学校周边餐饮、旅游景区餐饮、“两超一非”、网络订餐、农村食品市场等9项专项整治行动。  上半年全市累计出动监管人员90370人次,检查各类食品生产经营单位95813家次,查处食品案件798起,罚没款831.8万元,销毁不合格食品180吨。加强行政执法与刑事司法衔接,强化刑事责任追究,全市移送公安案件41件,采取强制措施58人。针对新兴的网络平台订餐情况,市食药监部门积极主动介入,开展网络订餐状况调研,约谈饿了么、百度外卖、美团外卖、零号线等5家主要第三方平台,签订《互联网订餐食品安全承诺书》,要求其把好餐饮单位上线准入关,严格履行食品安全主体责任。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  周伟强指出,各地政府和食品安全监管部门要准确把握食品安全工作的新要求,用新的发展理念指导食品安全工作,以社会共治的思路推进食品安全工作,继续推动落实“四有两责”,着力推进长效机制建设,严厉打击食品安全违法行为,强化监管和检测能力建设,进一步完善督查考评机制,切实保障人民群众“舌尖上的安全”。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\"><img src=\"http://mdm.ahd6g.cn/uploads/image/20170405/1491377317568195.jpg\" title=\"1491377317568195.jpg\" alt=\"timg (5).jpg\" width=\"100%\"/></p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  昆山、张家港、常熟、太仓四市及吴江区、相城区、苏州高新区三区正在参与江苏省首批省级食品安全城市创建,动员部署、组织创建阶段现已基本完成,市食安委将加强跟踪督导,组织相关人员按照创建标准开展督查,对发现的问题及时督促整改,迎接下一阶段的考核评价。周伟强要求,各地要用好“食品安全城市创建”这一有力抓手,充分发挥政府主导作用,调动各部门力量和资源,激发人民群众和社会各界参与创建的积极性、主动性,形成食品安全社会共治局面。</p><p style=\"margin-top: 25px; margin-bottom: 25px; padding: 0px; font-stretch: normal; font-size: 18px; line-height: 32.4px; font-family: 微软雅黑; color: rgb(34, 34, 34); white-space: normal;\">  副市长盛蕾出席会议。</p><p style=\"text-align: center;\"><video class=\"edui-upload-video vjs-default-skin video-js\" controls=\"\" preload=\"none\" poster=\"http://mdm.ahd6g.cn/uploads/images/20170407/14915313834964.jpg\" width=\"100%\" src=\"http://mdm.ahd6g.cn/uploads/videos/20170405/1491381800143098.mp4\" data-setup=\"{}\"><source src=\"http://mdm.ahd6g.cn/uploads/videos/20170405/1491381800143098.mp4\" type=\"video/mp4\"/></video></p>";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView= (WebView) findViewById(R.id.webview);
        mLayout = (FrameLayout) findViewById(R.id.fl_video);

        WebSettings settings = mWebView.getSettings();
        //         允许javascript的执行
        settings.setJavaScriptEnabled(true);
//        告诉JavaScript自动打开窗口。这适用于JavaScript函数的窗口。open()。
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
//      告诉webview 是否使用插件
        settings.setPluginState(WebSettings.PluginState.ON);
//        启用或禁用WebView中的文件访问
        settings.setAllowFileAccess(true);
//      是否调节内容 是否全屏
        settings.setLoadWithOverviewMode(true);
//        重写缓存使用的方式。      WebSettings.LOAD_NO_CACHE 不要使用缓存,从网络加载。
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
//        设置WebView的用户代理字符串
        settings.setUserAgentString(null);
//        设置渲染优先级
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.loadDataWithBaseURL(null, mStrHtml, "text/html", "utf-8", null);
    }

    class MyWebChromeClient extends WebChromeClient {
        private CustomViewCallback customViewCallback;

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view,callback);
            if (nVideoView != null) {
                callback.onCustomViewHidden();
                return;
            }
            nVideoView = view;
            nVideoView.setVisibility(View.VISIBLE);
            customViewCallback = callback;
            mLayout.addView(nVideoView);
            mLayout.setVisibility(View.VISIBLE);
            mLayout.bringToFront();
            //设置横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            //设置全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        // 退出全屏调用此函数
        @Override
        public void onHideCustomView() {
            if (nVideoView == null) {
                return;
            }
            try {
                customViewCallback.onCustomViewHidden();
            } catch (Exception e) {
            }
            nVideoView.setVisibility(View.GONE);
            mLayout.removeView(nVideoView);
            nVideoView = null;
            mLayout.setVisibility(View.GONE);
            // 设置竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            // 取消全屏
            final WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.onPause();
        mWebView.freeMemory();
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
    }

}

最后权限

<!--虽然是html文本 单视频要访问网络  所有添加 允许访问网络权限-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <!--  是控制Android系统设置等敏感信息的权限-->
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

还要给对应的acitivity中添加可旋转

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!--防止切屏后重新开始生命周期-->
        <!--android:configChanges="orientation|keyboardHidden|navigation|screenSize"-->
        <!--允许硬件加速 防止视频卡顿-->
        <!--android:hardwareAccelerated="true"-->
        <!--设定该活动的方向  允许横屏显示-->
        <!--android:screenOrientation="portrait"-->
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|navigation|screenSize"
            android:hardwareAccelerated="true"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

简单分享一个需求问题 基本思路是从网上的一篇博客看到的 博客地址实在找不到了 自己记录一下

猜你喜欢

转载自blog.csdn.net/qq_30596077/article/details/69668864
今日推荐