PlayHelper.java
10.2 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
*/
package com.wd.player.playerutil;
import android.content.res.Configuration;
import android.net.Uri;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.fragment.app.FragmentActivity;
import com.aliyun.player.IPlayer;
import com.wd.foundation.wdkit.system.DeviceUtil;
import com.wd.player.constant.PlayParameter;
import com.wd.player.widget.AliyunRenderView;
import com.wd.player.widget.AliyunScreenMode;
import com.wd.player.widget.VideoAndLivePlayerView;
/**
* 描述:
*
* @author : lvjinhui
* @since: 2022/6/22
*/
public class PlayHelper {
/**
* 精准seek开启判断逻辑:当视频时长小于5分钟的时候。
*/
private static final int ACCURATE = 5 * 60 * 1000;
/**
* 播放器,不带控制层
*/
private AliyunRenderView mAliyunRenderView;
/**
* 播放器,带控制层
*/
private VideoAndLivePlayerView playerView;
private FragmentActivity mContext;
public static final class PlayHelperCreator {
public static final PlayHelper playHelper = new PlayHelper();
}
public static PlayHelper getInstance() {
return PlayHelperCreator.playHelper;
}
/**
* 设置播放器view
*
* @param playerView
*/
public void setPlayerView(VideoAndLivePlayerView playerView) {
this.playerView = playerView;
}
public void setAliyunRenderView(AliyunRenderView mAliyunRenderView) {
this.mAliyunRenderView = mAliyunRenderView;
}
public void setContext(FragmentActivity mContext) {
this.mContext = mContext;
}
/**
* 判断是否开启精准seek
*/
public void isAutoAccurate(long position) {
if (mAliyunRenderView == null) {
return;
}
if (getDuration() <= ACCURATE) {
mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Accurate);
} else {
mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Inaccurate);
}
}
/**
* 获取视频时长
*
* @return 视频时长
*/
public int getDuration() {
if (mAliyunRenderView != null) {
return (int) mAliyunRenderView.getDuration();
}
return 0;
}
/**
* 目标位置计算算法
*
* @param duration 视频总时长
* @param currentPosition 当前播放位置
* @param deltaPosition 与当前位置相差的时长
* @return
*/
public static int getTargetPosition(long duration, long currentPosition, long deltaPosition) {
// seek步长
long finalDeltaPosition;
// 根据视频时长,决定seek步长
long totalMinutes = duration / 1000 / 60;
int hours = (int) (totalMinutes / 60);
int minutes = (int) (totalMinutes % 60);
// 视频时长为1小时以上,小屏和全屏的手势滑动最长为视频时长的十分之一
if (hours >= 1) {
finalDeltaPosition = deltaPosition / 10;
}// 视频时长为31分钟-60分钟时,小屏和全屏的手势滑动最长为视频时长五分之一
else if (minutes > 30) {
finalDeltaPosition = deltaPosition / 5;
}// 视频时长为11分钟-30分钟时,小屏和全屏的手势滑动最长为视频时长三分之一
else if (minutes > 10) {
finalDeltaPosition = deltaPosition / 3;
}// 视频时长为4-10分钟时,小屏和全屏的手势滑动最长为视频时长二分之一
else if (minutes > 3) {
finalDeltaPosition = deltaPosition / 2;
}// 视频时长为1秒钟至3分钟时,小屏和全屏的手势滑动最长为视频结束
else {
finalDeltaPosition = deltaPosition;
}
long targetPosition = finalDeltaPosition + currentPosition;
if (targetPosition < 0) {
targetPosition = 0;
}
if (targetPosition > duration) {
targetPosition = duration;
}
return (int) targetPosition;
}
/**
* 判断是否是本地资源
*
* @return
*/
public boolean isLocalSource() {
String scheme = null;
if ("vidsts".equals(PlayParameter.PLAY_PARAM_TYPE)) {
return false;
}
if ("localSource".equals(PlayParameter.PLAY_PARAM_TYPE)) {
Uri parse = Uri.parse(PlayParameter.PLAY_PARAM_URL_VIDEO);
scheme = parse.getScheme();
}
return scheme == null;
}
/**
* 判断是否是Url播放资源
*/
public boolean isUrlSource() {
String scheme = null;
if ("vidsts".equals(PlayParameter.PLAY_PARAM_TYPE)) {
return false;
} else {
Uri parse = Uri.parse(PlayParameter.PLAY_PARAM_URL_VIDEO);
scheme = parse.getScheme();
return scheme != null;
}
}
/**
* 播放器切换横竖屏
*/
public void updatePlayerViewMode() {
if (playerView != null && mContext != null) {
int orientation = mContext.getResources().getConfiguration().orientation;
WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
Window mWindow = mContext.getWindow();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置属性
mWindow.setAttributes(lp);
//不允许窗口扩展到屏幕之外 clear掉了
mWindow.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// 设置view的布局,宽高之类
ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
aliVcVideoViewLayoutParams.height = (ScreenUtils.getWidth(mContext) * 9 / 16);
aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 转到横屏,隐藏状态栏
if (!DeviceUtil.isStrangePhone()) {
//获得 WindowManager.LayoutParams 属性对象
//直接对它flags变量操作 LayoutParams.FLAG_FULLSCREEN 表示设置全屏
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置属性
mWindow.setAttributes(lp);
//意思大致就是 允许窗口扩展到屏幕之外
mWindow.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
// 设置view的布局,宽高
ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
}
}
}
/**
* 播放器切换横竖屏(图文详情,播放器宽高自定义)
*/
public void updateArticlePlayerViewMode() {
if (playerView != null && mContext != null) {
int orientation = mContext.getResources().getConfiguration().orientation;
WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
Window mWindow = mContext.getWindow();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置属性
mWindow.setAttributes(lp);
//不允许窗口扩展到屏幕之外 clear掉了
mWindow.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// 设置view的布局,宽高之类
ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 转到横屏,隐藏状态栏
if (!DeviceUtil.isStrangePhone()) {
//获得 WindowManager.LayoutParams 属性对象
//直接对它flags变量操作 LayoutParams.FLAG_FULLSCREEN 表示设置全屏
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置属性
mWindow.setAttributes(lp);
//意思大致就是 允许窗口扩展到屏幕之外
mWindow.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
// 设置view的布局,宽高
ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
}
}
}
public boolean isFullMode(){
if (getScreenMode() == AliyunScreenMode.Full){
return true;
}
return false;
}
private AliyunScreenMode getScreenMode() {
if (playerView != null) {
return playerView.getScreenMode();
}
return null;
}
/**
* 是否在播放
* @return
*/
public boolean isPlaying(){
if (playerView != null) {
return playerView.isPlaying();
}
return false;
}
public void setScreenMode(AliyunScreenMode screenMode){
if (playerView !=null){
playerView.setScreenMode(screenMode);
}
}
public void release(){
if (mContext != null){
mContext =null;
}
if (playerView !=null){
playerView.onDestroy();
}
if (mAliyunRenderView !=null){
mAliyunRenderView.release();
}
}
}