-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
MediaPlayerData.java
431 lines (371 loc) · 13.9 KB
/
MediaPlayerData.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package expo.modules.av.player;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import android.util.Log;
import android.util.Pair;
import android.view.Surface;
import org.unimodules.core.ModuleRegistry;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.HttpCookie;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import expo.modules.av.AVManagerInterface;
import expo.modules.av.AudioFocusNotAcquiredException;
class MediaPlayerData extends PlayerData implements
MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnCompletionListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnInfoListener,
MediaPlayer.OnSeekCompleteListener,
MediaPlayer.OnVideoSizeChangedListener {
static final String IMPLEMENTATION_NAME = "MediaPlayer";
private MediaPlayer mMediaPlayer = null;
private ModuleRegistry mModuleRegistry = null;
private boolean mMediaPlayerHasStartedEver = false;
private Integer mPlayableDurationMillis = null;
private boolean mIsBuffering = false;
MediaPlayerData(final AVManagerInterface avModule, final Context context, final Uri uri, final Map<String, Object> requestHeaders) {
super(avModule, uri, requestHeaders);
mModuleRegistry = avModule.getModuleRegistry();
}
@Override
String getImplementationName() {
return IMPLEMENTATION_NAME;
}
// --------- PlayerData implementation ---------
// Lifecycle
@Override
public void load(final Bundle status,
final LoadCompletionListener loadCompletionListener) {
if (mMediaPlayer != null) {
loadCompletionListener.onLoadError("Load encountered an error: MediaPlayerData cannot be loaded twice.");
return;
}
final MediaPlayer unpreparedPlayer = new MediaPlayer();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
unpreparedPlayer.setDataSource(mAVModule.getContext(), mUri, null, getHttpCookiesList());
} else {
Map<String, String> headers = new HashMap<>(1);
StringBuilder cookieBuilder = new StringBuilder();
for (HttpCookie httpCookie : getHttpCookiesList()) {
cookieBuilder.append(httpCookie.getName());
cookieBuilder.append("=");
cookieBuilder.append(httpCookie.getValue());
cookieBuilder.append("; ");
}
cookieBuilder.append("\r\n");
headers.put("Cookie", cookieBuilder.toString());
if (mRequestHeaders != null) {
for (Map.Entry<String, Object> headerEntry : mRequestHeaders.entrySet()) {
if (headerEntry.getValue() instanceof String) {
headers.put(headerEntry.getKey(), (String) headerEntry.getValue());
}
}
}
unpreparedPlayer.setDataSource(mAVModule.getContext(), mUri, headers);
}
} catch (final Throwable throwable) {
loadCompletionListener.onLoadError("Load encountered an error: setDataSource() threw an exception was thrown with message: " + throwable.toString());
return;
}
unpreparedPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(final MediaPlayer mp, final int what, final int extra) {
loadCompletionListener.onLoadError("Load encountered an error: the OnErrorListener was called with 'what' code " + what + " and 'extra' code " + extra + ".");
return true;
}
});
unpreparedPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
mMediaPlayer = mp;
mMediaPlayer.setOnBufferingUpdateListener(MediaPlayerData.this);
mMediaPlayer.setOnCompletionListener(MediaPlayerData.this);
mMediaPlayer.setOnErrorListener(MediaPlayerData.this);
mMediaPlayer.setOnInfoListener(MediaPlayerData.this);
setStatusWithListener(status, new SetStatusCompletionListener() {
@Override
public void onSetStatusComplete() {
loadCompletionListener.onLoadSuccess(getStatus());
}
@Override
public void onSetStatusError(final String error) {
loadCompletionListener.onLoadSuccess(getStatus());
}
});
}
});
try {
unpreparedPlayer.prepareAsync();
} catch (final Throwable throwable) {
loadCompletionListener.onLoadError("Load encountered an error: an exception was thrown from prepareAsync() with message: " + throwable.toString());
}
}
@Override
public synchronized void release() {
stopUpdatingProgressIfNecessary();
if (mMediaPlayer != null) {
mMediaPlayer.setOnBufferingUpdateListener(null);
mMediaPlayer.setOnCompletionListener(null);
mMediaPlayer.setOnErrorListener(null);
mMediaPlayer.setOnInfoListener(null);
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
boolean shouldContinueUpdatingProgress() {
return mMediaPlayer != null && !mIsBuffering;
}
// Set status
@RequiresApi(api = Build.VERSION_CODES.M)
private void playMediaPlayerWithRateMAndHigher(final float rate) {
final PlaybackParams params = mMediaPlayer.getPlaybackParams();
params.setPitch(mShouldCorrectPitch ? 1.0f : rate);
params.setSpeed(rate);
params.setAudioFallbackMode(PlaybackParams.AUDIO_FALLBACK_MODE_DEFAULT);
mMediaPlayer.setPlaybackParams(params);
mMediaPlayer.start();
}
@Override
void playPlayerWithRateAndMuteIfNecessary() throws AudioFocusNotAcquiredException {
if (mMediaPlayer == null || !shouldPlayerPlay()) {
return;
}
if (!mIsMuted) {
mAVModule.acquireAudioFocus();
}
updateVolumeMuteAndDuck();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
mMediaPlayerHasStartedEver = true;
}
} else {
boolean rateAndPitchAreSetCorrectly;
try {
final PlaybackParams params = mMediaPlayer.getPlaybackParams();
final float setRate = params.getSpeed();
final boolean setShouldCorrectPitch = params.getPitch() == 1.0f;
rateAndPitchAreSetCorrectly = setRate == mRate && setShouldCorrectPitch == mShouldCorrectPitch;
} catch (final Throwable throwable) {
rateAndPitchAreSetCorrectly = false;
}
if (mRate != 0 && (!mMediaPlayer.isPlaying() || !rateAndPitchAreSetCorrectly)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
playMediaPlayerWithRateMAndHigher(mRate);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Bizarrely, I wasn't able to change rate while a sound was playing unless I had
// changed the rate to something other than 1f before the sound started.
// This workaround seems to fix this issue (which is said to only be fixed in N):
// https://code.google.com/p/android/issues/detail?id=192135
playMediaPlayerWithRateMAndHigher(2f);
mMediaPlayer.pause();
playMediaPlayerWithRateMAndHigher(mRate);
}
mMediaPlayerHasStartedEver = true;
}
}
beginUpdatingProgressIfNecessary();
}
@Override
void applyNewStatus(final Integer newPositionMillis, final Boolean newIsLooping)
throws AudioFocusNotAcquiredException, IllegalStateException {
if (mMediaPlayer == null) {
throw new IllegalStateException("mMediaPlayer is null!");
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && mRate != 1.0f) {
Log.w("Expo MediaPlayerData", "Cannot set audio/video playback rate for Android SDK < 23.");
mRate = 1.0f;
}
// Set looping idempotently
if (newIsLooping != null) {
mMediaPlayer.setLooping(newIsLooping);
}
// Pause first if necessary.
if (!shouldPlayerPlay()) {
if (mMediaPlayerHasStartedEver) {
mMediaPlayer.pause();
}
stopUpdatingProgressIfNecessary();
}
// Mute / update volume if it doesn't require a request of the audio focus.
updateVolumeMuteAndDuck();
// Seek
if (newPositionMillis != null && newPositionMillis != mMediaPlayer.getCurrentPosition()) {
mMediaPlayer.seekTo(newPositionMillis);
}
// Play / unmute
playPlayerWithRateAndMuteIfNecessary();
}
// Get status
@Override
boolean isLoaded() {
return mMediaPlayer != null;
}
@Override
void getExtraStatusFields(final Bundle map) {
Integer duration = mMediaPlayer.getDuration();
duration = duration < 0 ? null : duration;
if (duration != null) {
map.putInt(STATUS_DURATION_MILLIS_KEY_PATH, duration);
}
map.putInt(STATUS_POSITION_MILLIS_KEY_PATH, getClippedIntegerForValue(mMediaPlayer.getCurrentPosition(), 0, duration));
if (mPlayableDurationMillis != null) {
map.putInt(STATUS_PLAYABLE_DURATION_MILLIS_KEY_PATH, getClippedIntegerForValue(mPlayableDurationMillis, 0, duration));
}
map.putBoolean(STATUS_IS_PLAYING_KEY_PATH, mMediaPlayer.isPlaying());
map.putBoolean(STATUS_IS_BUFFERING_KEY_PATH, mIsBuffering);
map.putBoolean(STATUS_IS_LOOPING_KEY_PATH, mMediaPlayer.isLooping());
}
// Video specific stuff
@Override
public Pair<Integer, Integer> getVideoWidthHeight() {
return mMediaPlayer == null ? new Pair<>(0, 0) : new Pair<>(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
}
@Override
public void tryUpdateVideoSurface(final Surface surface) {
if (mMediaPlayer == null) {
return;
}
mMediaPlayer.setSurface(surface);
if (!mMediaPlayerHasStartedEver && !mShouldPlay) {
// For some reason, the media player does not render to the screen until start() has been
// called in some cases.
mMediaPlayer.start();
mMediaPlayer.pause();
mMediaPlayerHasStartedEver = true;
}
}
@Override
public int getAudioSessionId() {
return mMediaPlayer != null ? mMediaPlayer.getAudioSessionId() : 0;
}
// --------- Interface implementation ---------
// AudioEventHandler
@Override
public void pauseImmediately() {
if (mMediaPlayer != null && mMediaPlayerHasStartedEver) {
mMediaPlayer.pause();
}
stopUpdatingProgressIfNecessary();
}
@Override
public boolean requiresAudioFocus() {
return mMediaPlayer != null && (mMediaPlayer.isPlaying() || shouldPlayerPlay()) && !mIsMuted;
}
@Override
public void updateVolumeMuteAndDuck() {
if (mMediaPlayer != null) {
final float value = mAVModule.getVolumeForDuckAndFocus(mIsMuted, mVolume);
mMediaPlayer.setVolume(value, value);
}
}
// MediaPlayer.*Listener
@Override
public void onBufferingUpdate(final MediaPlayer mp, final int percent) {
if (mp.getDuration() >= 0) {
mPlayableDurationMillis = (int) (mp.getDuration() * (((double) percent) / 100.0));
} else {
mPlayableDurationMillis = null;
}
callStatusUpdateListener();
}
@Override
public void onCompletion(final MediaPlayer mp) {
callStatusUpdateListenerWithDidJustFinish();
if (!mp.isLooping()) {
mAVModule.abandonAudioFocusIfUnused();
}
}
@Override
public boolean onError(final MediaPlayer mp, final int what, final int extra) {
release();
if (mErrorListener != null) {
mErrorListener.onError("MediaPlayer failed with 'what' code " + what + " and 'extra' code " + extra + ".");
}
return true;
}
@Override
public boolean onInfo(final MediaPlayer mp, final int what, final int extra) {
// Writing out all of the possible values here for clarity
// @jesseruder @nikki93 I think we should hold off on handling some of the more obscure values
// until the ExoPlayer refactor.
switch (what) {
case MediaPlayer.MEDIA_INFO_UNKNOWN:
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
mIsBuffering = true;
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
mIsBuffering = false;
beginUpdatingProgressIfNecessary();
break;
case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
break;
case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
break;
case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
break;
case MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE:
break;
case MediaPlayer.MEDIA_INFO_SUBTITLE_TIMED_OUT:
break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
if (mVideoSizeUpdateListener != null) {
mVideoSizeUpdateListener.onVideoSizeUpdate(new Pair<>(mp.getVideoWidth(), mp.getVideoHeight()));
}
break;
case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
break;
}
callStatusUpdateListener();
return true;
}
@Override
public void onSeekComplete(final MediaPlayer mp) {
callStatusUpdateListener();
}
@Override
public void onVideoSizeChanged(final MediaPlayer mp, final int width, final int height) {
if (mVideoSizeUpdateListener != null) {
mVideoSizeUpdateListener.onVideoSizeUpdate(new Pair<>(width, height));
}
}
// Utilities
private List<HttpCookie> getHttpCookiesList() {
if (mModuleRegistry != null) {
CookieHandler cookieHandler = mModuleRegistry.getModule(CookieHandler.class);
if (cookieHandler != null) {
try {
Map<String, List<String>> headersMap = cookieHandler.get(URI.create(mUri.toString()), null);
List<String> cookies = headersMap.get("Cookie");
if (cookies != null) {
List<HttpCookie> httpCookies = new ArrayList<>();
for (String cookieValue : cookies) {
httpCookies.addAll(HttpCookie.parse(cookieValue));
}
return httpCookies;
} else {
return Collections.emptyList();
}
} catch (IOException e) {
// do nothing, we'll return an empty list
}
}
}
return Collections.emptyList();
}
}