@@ -207,8 +207,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
207207 /// null. The [package] argument must be non-null when the asset comes from a
208208 /// package and null otherwise.
209209 VideoPlayerController .asset (this .dataSource,
210- {this .package, this .closedCaptionFile, this .videoPlayerOptions})
211- : dataSourceType = DataSourceType .asset,
210+ {this .package,
211+ Future <ClosedCaptionFile >? closedCaptionFile,
212+ this .videoPlayerOptions})
213+ : _closedCaptionFileFuture = closedCaptionFile,
214+ dataSourceType = DataSourceType .asset,
212215 formatHint = null ,
213216 httpHeaders = const < String , String > {},
214217 super (VideoPlayerValue (duration: Duration .zero));
@@ -225,10 +228,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
225228 VideoPlayerController .network (
226229 this .dataSource, {
227230 this .formatHint,
228- this . closedCaptionFile,
231+ Future < ClosedCaptionFile > ? closedCaptionFile,
229232 this .videoPlayerOptions,
230233 this .httpHeaders = const < String , String > {},
231- }) : dataSourceType = DataSourceType .network,
234+ }) : _closedCaptionFileFuture = closedCaptionFile,
235+ dataSourceType = DataSourceType .network,
232236 package = null ,
233237 super (VideoPlayerValue (duration: Duration .zero));
234238
@@ -237,8 +241,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
237241 /// This will load the file from the file-URI given by:
238242 /// `'file://${file.path}'` .
239243 VideoPlayerController .file (File file,
240- {this .closedCaptionFile, this .videoPlayerOptions})
241- : dataSource = 'file://${file .path }' ,
244+ {Future <ClosedCaptionFile >? closedCaptionFile, this .videoPlayerOptions})
245+ : _closedCaptionFileFuture = closedCaptionFile,
246+ dataSource = 'file://${file .path }' ,
242247 dataSourceType = DataSourceType .file,
243248 package = null ,
244249 formatHint = null ,
@@ -250,9 +255,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
250255 /// This will load the video from the input content-URI.
251256 /// This is supported on Android only.
252257 VideoPlayerController .contentUri (Uri contentUri,
253- {this . closedCaptionFile, this .videoPlayerOptions})
258+ {Future < ClosedCaptionFile > ? closedCaptionFile, this .videoPlayerOptions})
254259 : assert (defaultTargetPlatform == TargetPlatform .android,
255260 'VideoPlayerController.contentUri is only supported on Android.' ),
261+ _closedCaptionFileFuture = closedCaptionFile,
256262 dataSource = contentUri.toString (),
257263 dataSourceType = DataSourceType .contentUri,
258264 package = null ,
@@ -283,13 +289,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
283289 /// Only set for [asset] videos. The package that the asset was loaded from.
284290 final String ? package;
285291
286- /// Optional field to specify a file containing the closed
287- /// captioning.
288- ///
289- /// This future will be awaited and the file will be loaded when
290- /// [initialize()] is called.
291- final Future <ClosedCaptionFile >? closedCaptionFile;
292-
292+ Future <ClosedCaptionFile >? _closedCaptionFileFuture;
293293 ClosedCaptionFile ? _closedCaptionFile;
294294 Timer ? _timer;
295295 bool _isDisposed = false ;
@@ -397,9 +397,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
397397 }
398398 }
399399
400- if (closedCaptionFile != null ) {
401- _closedCaptionFile ?? = await closedCaptionFile;
402- value = value.copyWith (caption: _getCaptionAt (value.position));
400+ if (_closedCaptionFileFuture != null ) {
401+ await _updateClosedCaptionWithFuture (_closedCaptionFileFuture);
403402 }
404403
405404 void errorListener (Object obj) {
@@ -634,6 +633,28 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
634633 return Caption .none;
635634 }
636635
636+ /// Returns the file containing closed captions for the video, if any.
637+ Future <ClosedCaptionFile >? get closedCaptionFile {
638+ return _closedCaptionFileFuture;
639+ }
640+
641+ /// Sets a closed caption file.
642+ ///
643+ /// If [closedCaptionFile] is null, closed captions will be removed.
644+ Future <void > setClosedCaptionFile (
645+ Future <ClosedCaptionFile >? closedCaptionFile,
646+ ) async {
647+ await _updateClosedCaptionWithFuture (closedCaptionFile);
648+ _closedCaptionFileFuture = closedCaptionFile;
649+ }
650+
651+ Future <void > _updateClosedCaptionWithFuture (
652+ Future <ClosedCaptionFile >? closedCaptionFile,
653+ ) async {
654+ _closedCaptionFile = await closedCaptionFile;
655+ value = value.copyWith (caption: _getCaptionAt (value.position));
656+ }
657+
637658 void _updatePosition (Duration position) {
638659 value = value.copyWith (
639660 position: position,
0 commit comments