initialize method

Future<void> initialize()
inherited

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>();

  // Apply the web-specific options
  if (kIsWeb && videoPlayerOptions?.webOptions != null) {
    await _videoPlayerPlatform.setWebOptions(
      _textureId,
      videoPlayerOptions!.webOptions!,
    );
  }

  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,
        );
        assert(
          !initializingCompleter.isCompleted,
          'VideoPlayerController already initialized. This is typically a '
          'sign that an implementation of the VideoPlayerPlatform '
          '(${_videoPlayerPlatform.runtimeType}) has a bug and is sending '
          'more than one initialized event per instance.',
        );
        if (initializingCompleter.isCompleted) {
          throw StateError('VideoPlayerController already initialized');
        }
        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;
}