create method

  1. @override
Future<int?> create(
  1. DataSource dataSource
)
override

Creates an instance of a video player and returns its textureId.

Implementation

@override
Future<int?> create(DataSource dataSource) async {
  WinVideoPlayerController? controller;

  if (dataSource.sourceType == DataSourceType.file) {
    // dataSource.uri is url encoded and has a file:// scheme.
    // But if the dataSource.uri original path contains non-ASCII characters,
    // it will cause the IMFSourceResolver API url decoding to fail and cause
    // the app to crash.
    //
    // To avoid this, need to pass dataSource.uri to Uri.parse() and get
    // the path from uri.toFilePath(). It removes the file:// scheme and
    // url decodes the path.
    //
    // Without the file:// scheme, the IMFSourceResolver API treats the "%"
    // character as a normal string instead of url decoding the path.
    var uri = Uri.parse(dataSource.uri!);
    controller = WinVideoPlayerController.file(
      File(uri.toFilePath()),
      isPluginMode: true,
    );
  } else if (dataSource.sourceType == DataSourceType.network) {
    controller = WinVideoPlayerController.network(
      dataSource.uri!,
      isPluginMode: true,
      httpHeaders: dataSource.httpHeaders,
    );
  } else if (dataSource.sourceType == DataSourceType.asset) {
    controller = WinVideoPlayerController.asset(
      dataSource.asset!,
      isPluginMode: true,
    );
  } else {
    throw UnimplementedError(
      'create() has not been implemented for dataSource type [assets] and [contentUri] in Windows OS',
    );
  }

  await controller.initialize();
  if (controller.textureId_ > 0) {
    mControllerMap[controller.textureId_] = controller;
    return controller.textureId_;
  }
  return null;
}