Repository navigation
Expand file tree
/
Copy pathSubtitleDetailPlayer.java
More file actions
241 lines (209 loc) · 8.73 KB
/
Copy pathSubtitleDetailPlayer.java
File metadata and controls
241 lines (209 loc) · 8.73 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
package com.example.gsyvideoplayer;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.gsyvideoplayer.databinding.ActivityDetailSubtitlePlayerBinding;
import com.example.gsyvideoplayer.utils.DemoVideoUrls;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.subtitle.GSYSubtitleSource;
import com.shuyu.gsyvideoplayer.subtitle.GSYSubtitleStyle;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SubtitleDetailPlayer extends AppCompatActivity {
private boolean isPlay;
private boolean isPause;
private boolean subtitleEnabled = true;
private boolean largeSubtitle;
private OrientationUtils orientationUtils;
private ActivityDetailSubtitlePlayerBinding binding;
private List<GSYSubtitleSource> subtitleSources;
private int subtitleSourceIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerFactory.setPlayManager(IjkPlayerManager.class);
binding = ActivityDetailSubtitlePlayerBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.mipmap.xxx1);
binding.detailPlayer.getTitleTextView().setVisibility(View.GONE);
binding.detailPlayer.getBackButton().setVisibility(View.GONE);
orientationUtils = new OrientationUtils(this, binding.detailPlayer);
orientationUtils.setEnable(false);
Map<String, String> header = new HashMap<>();
header.put("allowCrossProtocolRedirects", "true");
new GSYVideoOptionBuilder()
.setThumbImageView(imageView)
.setIsTouchWiget(true)
.setRotateViewAuto(false)
.setLockLand(false)
.setShowFullAnimation(false)
.setNeedLockFull(true)
.setUrl(getUrl())
.setMapHeadData(header)
.setCacheWithPlay(false)
.setVideoTitle("通用字幕 IJK Demo")
.setVideoAllCallBack(new GSYSampleCallBack() {
@Override
public void onPrepared(String url, Object... objects) {
super.onPrepared(url, objects);
Debuger.printfError("***** subtitle ijk onPrepared **** " + objects[0]);
orientationUtils.setEnable(binding.detailPlayer.isRotateWithSystem());
isPlay = true;
}
@Override
public void onEnterFullscreen(String url, Object... objects) {
super.onEnterFullscreen(url, objects);
Debuger.printfError("***** subtitle ijk onEnterFullscreen **** " + objects[0]);
}
@Override
public void onQuitFullscreen(String url, Object... objects) {
super.onQuitFullscreen(url, objects);
Debuger.printfError("***** subtitle ijk onQuitFullscreen **** " + objects[0]);
if (orientationUtils != null) {
orientationUtils.backToProtVideo();
}
}
})
.setLockClickListener(new LockClickListener() {
@Override
public void onClick(View view, boolean lock) {
if (orientationUtils != null) {
orientationUtils.setEnable(!lock);
}
}
})
.build(binding.detailPlayer);
subtitleSources = createSubtitleSources();
binding.detailPlayer.setSubtitleSources(subtitleSources);
applySubtitleStyle(16);
binding.detailPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orientationUtils.resolveByClick();
binding.detailPlayer.startWindowFullscreen(SubtitleDetailPlayer.this, true, true);
}
});
binding.subtitleToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subtitleEnabled = !subtitleEnabled;
getCurPlay().setSubtitleEnabled(subtitleEnabled);
binding.subtitleToggle.setText(subtitleEnabled ? "GSY SUBTITLE OFF" : "GSY SUBTITLE ON");
}
});
binding.subtitleSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
largeSubtitle = !largeSubtitle;
int size = largeSubtitle ? 22 : 16;
getCurPlay().setSubtitleStyle(createSubtitleStyle(size));
binding.subtitleSize.setText(largeSubtitle ? "SUBTITLE SIZE 16" : "SUBTITLE SIZE 22");
}
});
binding.subtitleSource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (subtitleSources == null || subtitleSources.isEmpty()) {
return;
}
subtitleSourceIndex = (subtitleSourceIndex + 1) % subtitleSources.size();
GSYSubtitleSource source = subtitleSources.get(subtitleSourceIndex);
if (getCurPlay().selectSubtitle(source.getId())) {
binding.subtitleSource.setText("SUBTITLE " + source.getLabel());
}
}
});
}
@Override
protected void onPause() {
getCurPlay().onVideoPause();
super.onPause();
isPause = true;
}
@Override
protected void onResume() {
getCurPlay().onVideoResume(false);
super.onResume();
isPause = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isPlay) {
getCurPlay().release();
}
if (orientationUtils != null) {
orientationUtils.releaseListener();
}
}
@Override
public void onConfigurationChanged(@NonNull android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (isPlay && !isPause) {
binding.detailPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true);
}
}
private void applySubtitleStyle(int sizeSp) {
binding.detailPlayer.setSubtitleStyle(createSubtitleStyle(sizeSp));
}
private GSYSubtitleStyle createSubtitleStyle(int sizeSp) {
return new GSYSubtitleStyle.Builder()
.setTextColor(Color.WHITE)
.setTextSizeSp(sizeSp)
.setShadow(Color.BLACK, 3, 1, 1)
.setBottomMarginDp(56)
.build();
}
private GSYVideoPlayer getCurPlay() {
if (binding.detailPlayer.getFullWindowPlayer() != null) {
return binding.detailPlayer.getFullWindowPlayer();
}
return binding.detailPlayer;
}
private String getUrl() {
return DemoVideoUrls.MP4_BBB;
}
private List<GSYSubtitleSource> createSubtitleSources() {
List<GSYSubtitleSource> sources = new ArrayList<>();
sources.add(new GSYSubtitleSource.Builder(getLocalSrtSubtitleUri())
.setId("local-srt")
.setLabel("SRT LOCAL")
.setLanguage("zh")
.setDefault(true)
.build());
sources.add(new GSYSubtitleSource.Builder(getLocalVttSubtitleUri())
.setId("local-vtt")
.setLabel("VTT LOCAL")
.setLanguage("en")
.build());
sources.add(new GSYSubtitleSource.Builder(getNetworkSrtSubtitleUrl())
.setId("network-srt")
.setLabel("SRT NETWORK")
.setLanguage("zh")
.build());
return sources;
}
private String getLocalSrtSubtitleUri() {
return "android.resource://" + getPackageName() + "/" + R.raw.demo_subtitle;
}
private String getLocalVttSubtitleUri() {
return "android.resource://" + getPackageName() + "/" + R.raw.demo_subtitle_vtt;
}
private String getNetworkSrtSubtitleUrl() {
return DemoVideoUrls.SUBTITLE_SRT;
}
}