9
9
10
10
import android .content .Context ;
11
11
import android .net .Uri ;
12
+ import android .view .Surface ;
12
13
import androidx .annotation .NonNull ;
13
14
import androidx .annotation .VisibleForTesting ;
14
15
import com .google .android .exoplayer2 .C ;
@@ -47,37 +48,32 @@ final class VideoPlayer {
47
48
48
49
private ExoPlayer exoPlayer ;
49
50
50
- private TextureRegistry .SurfaceProducer surfaceProducer ;
51
+ private Surface surface ;
52
+
53
+ private final TextureRegistry .SurfaceTextureEntry textureEntry ;
51
54
52
55
private QueuingEventSink eventSink ;
53
56
54
57
private final EventChannel eventChannel ;
55
58
56
59
private static final String USER_AGENT = "User-Agent" ;
57
60
58
- private MediaSource mediaSource ;
59
-
60
61
@ VisibleForTesting boolean isInitialized = false ;
61
62
62
- // State that must be reset when the surface is re-created.
63
63
private final VideoPlayerOptions options ;
64
- private long restoreVideoLocation = 0 ;
65
- private int restoreRepeatMode = 0 ;
66
- private float restoreVolume = 0 ;
67
- private PlaybackParameters restorePlaybackParameters ;
68
64
69
65
private DefaultHttpDataSource .Factory httpDataSourceFactory = new DefaultHttpDataSource .Factory ();
70
66
71
67
VideoPlayer (
72
68
Context context ,
73
69
EventChannel eventChannel ,
74
- TextureRegistry .SurfaceProducer surfaceProducer ,
70
+ TextureRegistry .SurfaceTextureEntry textureEntry ,
75
71
String dataSource ,
76
72
String formatHint ,
77
73
@ NonNull Map <String , String > httpHeaders ,
78
74
VideoPlayerOptions options ) {
79
75
this .eventChannel = eventChannel ;
80
- this .surfaceProducer = surfaceProducer ;
76
+ this .textureEntry = textureEntry ;
81
77
this .options = options ;
82
78
83
79
ExoPlayer exoPlayer = new ExoPlayer .Builder (context ).build ();
@@ -87,7 +83,7 @@ final class VideoPlayer {
87
83
DataSource .Factory dataSourceFactory =
88
84
new DefaultDataSource .Factory (context , httpDataSourceFactory );
89
85
90
- mediaSource = buildMediaSource (uri , dataSourceFactory , formatHint );
86
+ MediaSource mediaSource = buildMediaSource (uri , dataSourceFactory , formatHint );
91
87
92
88
exoPlayer .setMediaSource (mediaSource );
93
89
exoPlayer .prepare ();
@@ -100,12 +96,12 @@ final class VideoPlayer {
100
96
VideoPlayer (
101
97
ExoPlayer exoPlayer ,
102
98
EventChannel eventChannel ,
103
- TextureRegistry .SurfaceProducer surfaceProducer ,
99
+ TextureRegistry .SurfaceTextureEntry textureEntry ,
104
100
VideoPlayerOptions options ,
105
101
QueuingEventSink eventSink ,
106
102
DefaultHttpDataSource .Factory httpDataSourceFactory ) {
107
103
this .eventChannel = eventChannel ;
108
- this .surfaceProducer = surfaceProducer ;
104
+ this .textureEntry = textureEntry ;
109
105
this .options = options ;
110
106
this .httpDataSourceFactory = httpDataSourceFactory ;
111
107
@@ -173,40 +169,6 @@ private MediaSource buildMediaSource(
173
169
}
174
170
}
175
171
176
- public void recreateSurface (Context context ) {
177
- ExoPlayer exoPlayer = new ExoPlayer .Builder (context ).build ();
178
-
179
- exoPlayer .setMediaSource (mediaSource );
180
- exoPlayer .prepare ();
181
-
182
- setUpVideoPlayer (exoPlayer , new QueuingEventSink ());
183
- exoPlayer .setVideoSurface (surfaceProducer .getSurface ());
184
- exoPlayer .seekTo (restoreVideoLocation );
185
- exoPlayer .setRepeatMode (restoreRepeatMode );
186
- exoPlayer .setVolume (restoreVolume );
187
- if (restorePlaybackParameters != null ) {
188
- exoPlayer .setPlaybackParameters (restorePlaybackParameters );
189
- }
190
- }
191
-
192
- public void pauseSurface () {
193
- if (!isInitialized ) {
194
- return ;
195
- }
196
- restoreVideoLocation = exoPlayer .getCurrentPosition ();
197
- restoreRepeatMode = exoPlayer .getRepeatMode ();
198
- restoreVolume = exoPlayer .getVolume ();
199
- restorePlaybackParameters = exoPlayer .getPlaybackParameters ();
200
- eventChannel .setStreamHandler (null );
201
- if (isInitialized ) {
202
- exoPlayer .stop ();
203
- }
204
- if (exoPlayer != null ) {
205
- exoPlayer .release ();
206
- }
207
- isInitialized = false ;
208
- }
209
-
210
172
private void setUpVideoPlayer (ExoPlayer exoPlayer , QueuingEventSink eventSink ) {
211
173
this .exoPlayer = exoPlayer ;
212
174
this .eventSink = eventSink ;
@@ -224,7 +186,8 @@ public void onCancel(Object o) {
224
186
}
225
187
});
226
188
227
- exoPlayer .setVideoSurface (surfaceProducer .getSurface ());
189
+ surface = new Surface (textureEntry .surfaceTexture ());
190
+ exoPlayer .setVideoSurface (surface );
228
191
setAudioAttributes (exoPlayer , options .mixWithOthers );
229
192
230
193
exoPlayer .addListener (
@@ -371,8 +334,11 @@ void dispose() {
371
334
if (isInitialized ) {
372
335
exoPlayer .stop ();
373
336
}
374
- surfaceProducer .release ();
337
+ textureEntry .release ();
375
338
eventChannel .setStreamHandler (null );
339
+ if (surface != null ) {
340
+ surface .release ();
341
+ }
376
342
if (exoPlayer != null ) {
377
343
exoPlayer .release ();
378
344
}
0 commit comments