initialize method

Future<void> initialize()

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

Implementation

Future<void> initialize() async {
  _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,
      );
    case DataSourceType.file:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.file,
        uri: dataSource,
      );
    case DataSourceType.contentUri:
      dataSourceDescription = DataSource(
        sourceType: DataSourceType.contentUri,
        uri: dataSource,
      );
  }

  final platformViewType =
      Platform.isIOS ? VideoViewType.platformView : viewType;

  final VideoCreationOptions creationOptions = VideoCreationOptions(
    dataSource: dataSourceDescription,
    viewType: platformViewType,
  );

  _playerId =
      (await _videoPlayerPlatform.createWithOptions(creationOptions)) ??
      kUninitializedPlayerId;
  _creatingCompleter!.complete(null);
  final Completer<void> initializingCompleter = Completer<void>();

  void eventListener(VideoEvent event) {
    switch (event.eventType) {
      case VideoEventType.initialized:
        value = value.copyWith(
          duration: event.duration,
          size: event.size,
          isInitialized: event.duration != null,
          isCompleted: false,
        );
        initializingCompleter.complete(null);
        _videoPlayerPlatform.setVolume(_playerId, 1.0);
        _videoPlayerPlatform.setLooping(_playerId, true);
        _applyPlayPause();
      case VideoEventType.completed:
        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;
    }
  }

  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(_playerId)
      .listen(eventListener, onError: errorListener);
  return initializingCompleter.future;
}