prepareMediaResources method

  1. @override
Future<void> prepareMediaResources(
  1. {required AppModel appModel,
  2. required WidgetRef ref,
  3. required MediaItem item}
)
override

It may be useful to do multiple operations at the same time, so this is used before preparing the controller and subtitles.

Implementation

@override
Future<void> prepareMediaResources({
  required AppModel appModel,
  required WidgetRef ref,
  required MediaItem item,
}) async {
  String videoId = VideoId(item.mediaIdentifier).value;
  bool subtitlesCached = getSubtitleItems(videoId) != null &&
      getSubtitleMetadata(videoId) != null;
  bool streamCached = _streamManifestCache[videoId] != null;

  if (!subtitlesCached || !streamCached) {
    ComputeManifestParams params = ComputeManifestParams(
      videoId: videoId,
      subtitlesCached: subtitlesCached,
      closedCaptionsManifest: _closedCaptionManifestCache[videoId],
    );
    VideoManifest videoManifest = await compute(computeManifests, params);
    _streamManifestCache[videoId] = videoManifest.streamManifest;
    _closedCaptionManifestCache[videoId] ??=
        videoManifest.closedCaptionManifest;

    if (!subtitlesCached) {
      setSubtitleMetadata(
        videoId: videoId,
        metadata: videoManifest.subtitlesByLanguageCache.keys.toList(),
      );
      setSubtitleItems(
        videoId: videoId,
        subtitles: videoManifest.subtitlesByLanguageCache.values.toList(),
      );
    }
  }
}