initialize method

Future<void> initialize()

Attempts to open the given dataSource and load metadata about the video.

Implementation

Future<void> initialize() async {
  _lifeCycleObserver = _CachedVideoAppLifeCycleObserver(this);
  _lifeCycleObserver.initialize();
  _creatingCompleter = Completer<void>();

  late DataSource dataSourceDescription;
  switch (dataSourceType) {
    case DataSourceType.asset:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.asset,
        asset: dataSource,
        package: package,
      );
      break;
    case DataSourceType.network:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.network,
        uri: dataSource,
        formatHint: formatHint,
        httpHeaders: httpHeaders,
      );
      break;
    case DataSourceType.file:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.file,
        uri: dataSource,
      );
      break;
    case DataSourceType.contentUri:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.contentUri,
        uri: dataSource,
      );
      break;
  }

  if (videoPlayerOptions?.mixWithOthers != null) {
    await _videoPlayerPlatform
        .setMixWithOthers(videoPlayerOptions!.mixWithOthers);
  }

  _textureId = (await _videoPlayerPlatform.create(dataSourceDescription)) ??
      kUninitializedTextureId;
  _creatingCompleter!.complete(null);
  final Completer<void> initializingCompleter = Completer<void>();

  void eventListener(VideoEvent event) {
    if (_isDisposed) {
      return;
    }

    switch (event.eventType) {
      case VideoEventType.initialized:
        value = value.copyWith(
          duration: event.duration,
          size: event.size,
          isInitialized: event.duration != null,
        );
        initializingCompleter.complete(null);
        _applyLooping();
        _applyVolume();
        _applyPlayPause();
        break;
      case VideoEventType.completed:
        value = value.copyWith(isPlaying: false, position: value.duration);
        _timer?.cancel();
        break;
      case VideoEventType.bufferingUpdate:
        value = value.copyWith(buffered: event.buffered);
        break;
      case VideoEventType.bufferingStart:
        value = value.copyWith(isBuffering: true);
        break;
      case VideoEventType.bufferingEnd:
        value = value.copyWith(isBuffering: false);
        break;
      case VideoEventType.unknown:
        break;
    }
  }

  if (closedCaptionFile != null) {
    if (_closedCaptionFile == null) {
      _closedCaptionFile = await closedCaptionFile;
    }
    value = value.copyWith(caption: _getCaptionAt(value.position));
  }

  void errorListener(Object obj) {
    final PlatformException e = obj as PlatformException;
    value = CachedVideoPlayerValue.erroneous(e.message!);
    _timer?.cancel();
    if (!initializingCompleter.isCompleted) {
      initializingCompleter.completeError(obj);
    }
  }

  _eventSubscription = _videoPlayerPlatform
      .videoEventsFor(_textureId)
      .listen(eventListener, onError: errorListener);
  return initializingCompleter.future;
}