PlayHelper.java
7.37 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
/*
* 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.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import androidx.fragment.app.FragmentActivity;
import com.aliyun.player.IPlayer;
import com.wd.player.constant.PlayParameter;
import com.wd.player.widget.AliyunRenderView;
import com.wd.player.widget.AliyunScreenMode;
import com.wd.player.widget.AliyunVodPlayerView;
import com.wd.common.utils.ToolsUtil;
/**
* 描述:
*
* @author : lvjinhui
* @since: 2022/6/22
*/
public class PlayHelper {
/**
* 精准seek开启判断逻辑:当视频时长小于5分钟的时候。
*/
private static final int ACCURATE = 5 * 60 * 1000;
/**
* 播放器,不带控制层
*/
private AliyunRenderView mAliyunRenderView;
/**
* 播放器,带控制层
*/
private AliyunVodPlayerView 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(AliyunVodPlayerView 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);
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);
scheme = parse.getScheme();
return scheme != null;
}
}
/**
* 播放器全屏
*/
public void updatePlayerViewMode() {
if (playerView != null && mContext != null) {
int orientation = mContext.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
// 设置view的布局,宽高之类
RelativeLayout.LayoutParams aliVcVideoViewLayoutParams =
(RelativeLayout.LayoutParams) playerView.getLayoutParams();
aliVcVideoViewLayoutParams.height = (int) (ScreenUtils.getWidth(mContext) * 9.0f / 16);
aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 转到横屏了。
// 隐藏状态栏
if (!ToolsUtil.isStrangePhone()) {
mContext.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
playerView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
// 设置view的布局,宽高
RelativeLayout.LayoutParams aliVcVideoViewLayoutParams =
(RelativeLayout.LayoutParams) 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;
}
public void setScreenMode(AliyunScreenMode screenMode){
if (playerView !=null){
playerView.setScreenMode(screenMode);
}
}
}