27
27
import com .google .android .exoplayer2 .DefaultRenderersFactory ;
28
28
import com .google .android .exoplayer2 .ExoPlaybackException ;
29
29
import com .google .android .exoplayer2 .Format ;
30
+ import com .google .android .exoplayer2 .MediaItem ;
30
31
import com .google .android .exoplayer2 .PlaybackParameters ;
31
32
import com .google .android .exoplayer2 .Player ;
32
33
import com .google .android .exoplayer2 .RenderersFactory ;
48
49
import com .google .android .exoplayer2 .upstream .DefaultHttpDataSource ;
49
50
import com .google .android .exoplayer2 .upstream .DefaultHttpDataSourceFactory ;
50
51
import com .google .android .exoplayer2 .upstream .RawResourceDataSource ;
52
+ import com .google .android .exoplayer2 .util .MimeTypes ;
51
53
import com .google .android .exoplayer2 .util .Util ;
52
54
import com .google .android .exoplayer2 .video .VideoListener ;
53
55
import com .kk .taurus .playerbase .config .AppContextAttach ;
66
68
67
69
import java .util .HashMap ;
68
70
71
+ import static com .google .android .exoplayer2 .util .Assertions .checkNotNull ;
72
+
69
73
public class ExoMediaPlayer extends BaseInternalPlayer {
70
74
71
75
private final String TAG = "ExoMediaPlayer" ;
@@ -181,15 +185,17 @@ public void setDataSource(DataSource dataSource) {
181
185
//handle timed text source
182
186
TimedTextSource timedTextSource = dataSource .getTimedTextSource ();
183
187
if (timedTextSource !=null ){
184
- Format format = Format .createTextSampleFormat ( null , timedTextSource .getMimeType (), timedTextSource .getFlag (), null );
188
+ Format format = new Format .Builder (). setSampleMimeType ( timedTextSource .getMimeType ()). setSelectionFlags ( timedTextSource .getFlag ()). build ( );
185
189
MediaSource timedTextMediaSource = new SingleSampleMediaSource .Factory (new DefaultDataSourceFactory (mAppContext ,
186
- userAgent ))
187
- .createMediaSource (Uri .parse (timedTextSource .getPath ()), format , C .TIME_UNSET );
190
+ userAgent )).createMediaSource (new MediaItem .Subtitle (
191
+ Uri .parse (timedTextSource .getPath ()),
192
+ checkNotNull (format .sampleMimeType ), format .language , format .selectionFlags ), C .TIME_UNSET );
188
193
//merge MediaSource and timedTextMediaSource.
189
194
mediaSource = new MergingMediaSource (mediaSource , timedTextMediaSource );
190
195
}
191
196
192
- mInternalPlayer .prepare (mediaSource );
197
+ mInternalPlayer .setMediaSource (mediaSource );
198
+ mInternalPlayer .prepare ();
193
199
mInternalPlayer .setPlayWhenReady (false );
194
200
195
201
Bundle sourceBundle = BundlePool .obtain ();
@@ -200,17 +206,21 @@ public void setDataSource(DataSource dataSource) {
200
206
201
207
private MediaSource getMediaSource (Uri uri , com .google .android .exoplayer2 .upstream .DataSource .Factory dataSourceFactory ){
202
208
int contentType = Util .inferContentType (uri );
209
+ MediaItem mediaItem = new MediaItem .Builder ()
210
+ .setUri (uri )
211
+ .setMimeType (MimeTypes .APPLICATION_MPD )
212
+ .build ();
203
213
switch (contentType ) {
204
214
case C .TYPE_DASH :
205
- return new DashMediaSource .Factory (dataSourceFactory ).createMediaSource (uri );
215
+ return new DashMediaSource .Factory (dataSourceFactory ).createMediaSource (mediaItem );
206
216
case C .TYPE_SS :
207
- return new SsMediaSource .Factory (dataSourceFactory ).createMediaSource (uri );
217
+ return new SsMediaSource .Factory (dataSourceFactory ).createMediaSource (mediaItem );
208
218
case C .TYPE_HLS :
209
- return new HlsMediaSource .Factory (dataSourceFactory ).createMediaSource (uri );
219
+ return new HlsMediaSource .Factory (dataSourceFactory ).createMediaSource (mediaItem );
210
220
case C .TYPE_OTHER :
211
221
default :
212
222
// This is the MediaSource representing the media to be played.
213
- return new ProgressiveMediaSource .Factory (dataSourceFactory ).createMediaSource (uri );
223
+ return new ProgressiveMediaSource .Factory (dataSourceFactory ).createMediaSource (mediaItem );
214
224
}
215
225
}
216
226
@@ -290,8 +300,13 @@ public void start() {
290
300
291
301
@ Override
292
302
public void start (int msc ) {
293
- mStartPos = msc ;
294
- start ();
303
+ if (getState () == STATE_PREPARED && msc > 0 ){
304
+ start ();
305
+ seekTo (msc );
306
+ }else {
307
+ mStartPos = msc ;
308
+ start ();
309
+ }
295
310
}
296
311
297
312
@ Override
@@ -387,7 +402,7 @@ public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray tra
387
402
}
388
403
389
404
@ Override
390
- public void onLoadingChanged (boolean isLoading ) {
405
+ public void onIsLoadingChanged (boolean isLoading ) {
391
406
int bufferPercentage = mInternalPlayer .getBufferedPercentage ();
392
407
if (!isLoading ){
393
408
submitBufferingUpdate (bufferPercentage , null );
@@ -396,25 +411,8 @@ public void onLoadingChanged(boolean isLoading) {
396
411
}
397
412
398
413
@ Override
399
- public void onPlayerStateChanged (boolean playWhenReady , int playbackState ) {
400
- PLog .d (TAG ,"onPlayerStateChanged : playWhenReady = " + playWhenReady
401
- + ", playbackState = " + playbackState );
402
-
403
- if (!isPreparing ){
404
- if (playWhenReady ){
405
- if (getState ()==STATE_PREPARED ){
406
- updateStatus (IPlayer .STATE_STARTED );
407
- submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_AUDIO_RENDER_START , null );
408
- }else {
409
- updateStatus (IPlayer .STATE_STARTED );
410
- submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_RESUME , null );
411
- }
412
- }else {
413
- updateStatus (IPlayer .STATE_PAUSED );
414
- submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_PAUSE , null );
415
- }
416
- }
417
-
414
+ public void onPlaybackStateChanged (int playbackState ) {
415
+ PLog .d (TAG ,"onPlayerStateChanged : playbackState = " + playbackState );
418
416
if (isPreparing ){
419
417
switch (playbackState ){
420
418
case Player .STATE_READY :
@@ -428,19 +426,13 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
428
426
updateStatus (IPlayer .STATE_PREPARED );
429
427
submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_PREPARED , bundle );
430
428
431
- if (playWhenReady ){
432
- updateStatus (STATE_STARTED );
433
- submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_AUDIO_RENDER_START , null );
434
- }
435
-
436
429
if (mStartPos > 0 && mInternalPlayer .getDuration () > 0 ){
437
430
mInternalPlayer .seekTo (mStartPos );
438
431
mStartPos = -1 ;
439
432
}
440
433
break ;
441
434
}
442
435
}
443
-
444
436
if (isBuffering ){
445
437
switch (playbackState ){
446
438
case Player .STATE_READY :
@@ -480,7 +472,25 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
480
472
break ;
481
473
}
482
474
}
475
+ }
483
476
477
+ @ Override
478
+ public void onPlayWhenReadyChanged (boolean playWhenReady , int reason ) {
479
+ PLog .d (TAG ,"onPlayerStateChanged : playWhenReady = " + playWhenReady + ", reason = " + reason );
480
+ if (!isPreparing ){
481
+ if (playWhenReady ){
482
+ if (getState ()==STATE_PREPARED ){
483
+ updateStatus (IPlayer .STATE_STARTED );
484
+ submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_AUDIO_RENDER_START , null );
485
+ }else {
486
+ updateStatus (IPlayer .STATE_STARTED );
487
+ submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_RESUME , null );
488
+ }
489
+ }else {
490
+ updateStatus (IPlayer .STATE_PAUSED );
491
+ submitPlayerEvent (OnPlayerEventListener .PLAYER_EVENT_ON_PAUSE , null );
492
+ }
493
+ }
484
494
}
485
495
486
496
@ Override
0 commit comments