admin
2022-01-07 8dfe5354073b700af45d5cb472dd5f003e6f3f25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.ysvideo.zhibo.app.ui.video;
 
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
 
 
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.ysvideo.zhibo.app.R;
import com.ysvideo.zhibo.app.ui.subview.X5WebView;
import com.ysvideo.zhibo.lib.common.RetainViewFragment;
 
/**
 * 专题碎片
 */
public class VideoWebPlayerFragment extends RetainViewFragment {
 
    private String playUrl;
    private String url;
    private String title;
 
    private X5WebView webview;
 
    private ImageView iv_back;
 
    public static VideoWebPlayerFragment newInstance(String title, String url, String playUrl) {
        VideoWebPlayerFragment fragment = new VideoWebPlayerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        bundle.putString("title", title);
        bundle.putString("playUrl", playUrl);
        fragment.setArguments(bundle);
        return fragment;
    }
 
    public void setVideoInfo(String title, String url, String playUrl) {
        this.title = title;
        this.url = url;
        this.playUrl = playUrl;
        webview.loadUrl(playUrl);
    }
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_web_player;
    }
 
 
    private void initPlayer() {
    }
 
    private void initX5WebView(View view) {
        webview = view.findViewById(R.id.webview);
        webview.setWebChromeClient(new WebChromeClient() {
 
            View myVideoView;
            View myNormalView;
            IX5WebChromeClient.CustomViewCallback callback;
 
            /**
             * 全屏播放配置
             */
            @Override
            public void onShowCustomView(View view,
                                         IX5WebChromeClient.CustomViewCallback customViewCallback) {
                ViewGroup viewGroup = (ViewGroup) webview.getParent();
                viewGroup.removeView(webview);
                viewGroup.addView(view);
                myVideoView = view;
                myNormalView = webview;
                callback = customViewCallback;
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                getActivity().getWindow().setAttributes(attrs);
            }
 
            @Override
            public void onHideCustomView() {
                if (callback != null) {
                    callback.onCustomViewHidden();
                    callback = null;
                }
                if (myVideoView != null) {
                    ViewGroup viewGroup = (ViewGroup) myVideoView.getParent();
                    viewGroup.removeView(myVideoView);
                    viewGroup.addView(myNormalView);
                }
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                getActivity().getWindow().setAttributes(attrs);
            }
 
        });
 
 
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setTextZoom(100);
    }
 
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
 
        url = getArguments().getString("url", "");
        playUrl = getArguments().getString("playUrl", "");
        title = getArguments().getString("title", "");
        iv_back = contentView.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
 
                if (getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    getActivity().finish();
                }
            }
        });
        initPlayer();
        initX5WebView(contentView);
        webview.loadUrl(playUrl);
    }
 
 
    public void onBackPressed() {
        //先返回正常状态
        if (getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            iv_back.performClick();
            return;
        }
        //释放所有
//        videoPlayer.setVideoAllCallBack(null);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        webview.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        webview.onPause();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}