-
Notifications
You must be signed in to change notification settings - Fork 24
/
TubiPlayerActivity.java
271 lines (238 loc) · 10.4 KB
/
TubiPlayerActivity.java
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
package com.tubitv.media.activities;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MergingMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.tubitv.media.R;
import com.tubitv.media.helpers.MediaHelper;
import com.tubitv.media.interfaces.PlaybackActionCallback;
import com.tubitv.media.interfaces.TubiPlaybackControlInterface;
import com.tubitv.media.models.MediaModel;
import com.tubitv.media.utilities.EventLogger;
import com.tubitv.media.utilities.Utils;
import com.tubitv.media.views.TubiExoPlayerView;
/**
* This is the base activity that prepare one instance of {@link SimpleExoPlayer} mMoviePlayer, this player is mean to serve as the main player to player content.
* Along with some abstract methods to be implemented by subclass for extra functions.
* You can use this class as it is and implement the abstract methods to be a standalone player to player video with customized UI controls and different forms of adaptive streaming.
*/
public abstract class TubiPlayerActivity extends LifeCycleActivity
implements PlaybackActionCallback {
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
public static String TUBI_MEDIA_KEY = "tubi_media_key";
protected SimpleExoPlayer mMoviePlayer;
protected TubiExoPlayerView mTubiPlayerView;
protected WebView vpaidWebView;
protected TextView cuePointIndictor;
protected DefaultTrackSelector mTrackSelector;
protected boolean isActive = false;
/**
* ideally, only one instance of {@link MediaModel} and its arrtibute {@link MediaSource} for movie should be created throughout the whole movie playing experiences.
*/
@NonNull
protected MediaModel mediaModel;
private Handler mMainHandler;
private DataSource.Factory mMediaDataSourceFactory;
private EventLogger mEventLogger;
public abstract View addUserInteractionView();
protected abstract void onPlayerReady();
protected abstract void updateResumePosition();
protected abstract boolean isCaptionPreferenceEnable();
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Utils.hideSystemUI(this, true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parseIntent();
Utils.hideSystemUI(this, true);
mMediaDataSourceFactory = buildDataSourceFactory(true);
initLayout();
}
@Override
public void onNewIntent(Intent intent) {
releaseMoviePlayer();
setIntent(intent);
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
setupExo();
}
}
@Override
public void onResume() {
super.onResume();
if ((Util.SDK_INT <= 23 || mMoviePlayer == null)) {
setupExo();
}
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
releaseMoviePlayer();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
releaseMoviePlayer();
}
}
@Override
public boolean isActive() {
return isActive;
}
@SuppressWarnings("ConstantConditions")
protected void parseIntent() {
String errorNoMediaMessage = getResources().getString(R.string.activity_tubi_player_no_media_error_message);
Assertions.checkState(getIntent() != null && getIntent().getExtras() != null,
errorNoMediaMessage);
mediaModel = (MediaModel) getIntent().getExtras().getSerializable(TUBI_MEDIA_KEY);
Assertions.checkState(mediaModel != null,
errorNoMediaMessage);
}
protected void initLayout() {
setContentView(R.layout.activity_tubi_player);
mTubiPlayerView = (TubiExoPlayerView) findViewById(R.id.tubitv_player);
mTubiPlayerView.requestFocus();
vpaidWebView = (WebView) findViewById(R.id.vpaid_webview);
vpaidWebView.setBackgroundColor(Color.BLACK);
cuePointIndictor = (TextView) findViewById(R.id.cuepoint_indictor);
mTubiPlayerView.addUserInteractionView(addUserInteractionView());
}
private void setCaption(boolean isOn) {
if (mediaModel != null && mediaModel.getSubtitlesUrl() != null && mTubiPlayerView != null
&& mTubiPlayerView.getControlView() != null) {
mTubiPlayerView.getPlayerController().triggerSubtitlesToggle(isOn);
}
}
private void setupExo() {
initMoviePlayer();
setCaption(isCaptionPreferenceEnable());
isActive = true;
onPlayerReady();
}
protected void initMoviePlayer() {
// 1. Create a default TrackSelector
mMainHandler = new Handler();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
mTrackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
// 3. Create the mMoviePlayer
mMoviePlayer = ExoPlayerFactory.newSimpleInstance(this, mTrackSelector);
mEventLogger = new EventLogger(mTrackSelector);
mMoviePlayer.addAnalyticsListener(mEventLogger);
mMoviePlayer.addMetadataOutput(mEventLogger);
mTubiPlayerView.setPlayer(mMoviePlayer, this);
mTubiPlayerView.setMediaModel(mediaModel);
}
protected void releaseMoviePlayer() {
if (mMoviePlayer != null) {
updateResumePosition();
mMoviePlayer.release();
mMoviePlayer = null;
mTrackSelector = null;
}
isActive = false;
}
protected MediaSource buildMediaSource(MediaModel model) {
MediaSource mediaSource;
int type = TextUtils.isEmpty(model.getMediaExtension()) ? Util.inferContentType(model.getVideoUrl())
: Util.inferContentType("." + model.getMediaExtension());
// TODO: Replace deprecated constructors with proper factory
switch (type) {
case C.TYPE_SS:
mediaSource = new SsMediaSource(model.getVideoUrl(), buildDataSourceFactory(false),
new DefaultSsChunkSource.Factory(mMediaDataSourceFactory), mMainHandler, mEventLogger);
break;
case C.TYPE_DASH:
mediaSource = new DashMediaSource(model.getVideoUrl(), buildDataSourceFactory(false),
new DefaultDashChunkSource.Factory(mMediaDataSourceFactory), mMainHandler, mEventLogger);
break;
case C.TYPE_HLS:
mediaSource = new HlsMediaSource(model.getVideoUrl(), mMediaDataSourceFactory, mMainHandler,
mEventLogger);
break;
case C.TYPE_OTHER:
mediaSource = new ExtractorMediaSource(model.getVideoUrl(), mMediaDataSourceFactory,
new DefaultExtractorsFactory(),
mMainHandler, mEventLogger);
break;
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
if (model.getSubtitlesUrl() != null) {
MediaSource subtitleSource = new SingleSampleMediaSource(
model.getSubtitlesUrl(),
buildDataSourceFactory(false),
Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP, null, Format.NO_VALUE,
C.SELECTION_FLAG_DEFAULT, "en", null, 0),
0);
// Plays the video with the sideloaded subtitle.
mediaSource =
new MergingMediaSource(mediaSource, subtitleSource);
}
return mediaSource;
}
/**
* Returns a new DataSource factory.MainActivity
*
* @param useBandwidthMeter Whether to set {@link #BANDWIDTH_METER} as a listener to the new
* DataSource factory.
* @return A new DataSource factory.
*/
protected DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
return MediaHelper.buildDataSourceFactory(this, useBandwidthMeter ? BANDWIDTH_METER : null);
}
// @Override
// public void onVisibilityChange(int visibility) {
//
// }
public HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
return new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "TubiPlayerActivity"), bandwidthMeter);
}
protected TubiPlaybackControlInterface getPlayerController() {
if (mTubiPlayerView != null && mTubiPlayerView.getPlayerController() != null) {
return mTubiPlayerView.getPlayerController();
}
return null;
}
}