initialize method

Future<void> initialize()

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

Implementation

Future<void> initialize() async {
  final bool allowBackgroundPlayback =
      videoPlayerOptions?.allowBackgroundPlayback ?? false;
  if (!allowBackgroundPlayback) {
    _lifeCycleObserver = _VideoAppLifeCycleObserver(this);
  }
  _lifeCycleObserver?.initialize();
  _creatingCompleter = Completer<void>();

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

  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,
          rotationCorrection: event.rotationCorrection,
          isInitialized: event.duration != null,
          errorDescription: null,
          isCompleted: false,
        );
        initializingCompleter.complete(null);
        _applyLooping();
        _applyVolume();
        _applyPlayPause();
      case VideoEventType.completed:
        // In this case we need to stop _timer, set isPlaying=false, and
        // position=value.duration. Instead of setting the values directly,
        // we use pause() and seekTo() to ensure the platform stops playing
        // and seeks to the last frame of the video.
        pause().then((void pauseResult) => seekTo(value.duration));
        value = value.copyWith(isCompleted: true);
      case VideoEventType.bufferingUpdate:
        value = value.copyWith(buffered: event.buffered);
      case VideoEventType.bufferingStart:
        value = value.copyWith(isBuffering: true);
      case VideoEventType.bufferingEnd:
        value = value.copyWith(isBuffering: false);
      case VideoEventType.isPlayingStateUpdate:
        if (event.isPlaying ?? false) {
          value =
              value.copyWith(isPlaying: event.isPlaying, isCompleted: false);
        } else {
          value = value.copyWith(isPlaying: event.isPlaying);
        }
      case VideoEventType.unknown:
        break;
    }
  }

  if (_closedCaptionFileFuture != null) {
    await _updateClosedCaptionWithFuture(_closedCaptionFileFuture);
  }

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

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