getControllerForVideo method

  1. @override
Future<VideoPlayerController> getControllerForVideo(
  1. VideoModel videoModel,
  2. bool isCaching
)
override

Gets a VideoPlayerController for a given video model.

videoModel contains the video URL and additional configuration options. isCaching determines whether the video should be cached for future use.

Returns a VideoPlayerController configured for the given video.

Implementation

@override
Future<VideoPlayerController> getControllerForVideo(
  VideoModel videoModel,
  bool isCaching,
) async {
  final url = videoModel.url;
  final headers = videoModel.httpHeaders ?? {};

  if (isCaching) {
    try {
      // Start proxy server if not running
      if (!_proxyServer.isRunning) {
        await _proxyServer.start();
      }

      // Register the URL with the proxy server and pass headers
      final proxyUrl = await _proxyServer.registerUrl(url, headers: headers);

      log('Playing video through proxy: $proxyUrl');

      // When using proxy server, we don't need to include headers in VideoPlayerController
      // since they were already passed to the proxy server
      return VideoPlayerController.networkUrl(
        Uri.parse(proxyUrl),
        // No need to include headers here as they're handled by the proxy
        videoPlayerOptions: videoModel.videoPlayerOptions,
      );
    } catch (e) {
      // Log error if encountered while setting up proxy for video
      log('Error setting up proxy for video: $e', error: e);
      // Fallback to direct network URL if proxy fails
    }
  }

  // Default to direct network URL if caching is disabled or if proxy setup failed
  log('Playing video directly: $url');
  return VideoPlayerController.networkUrl(
    Uri.parse(url),
    httpHeaders: headers,
    videoPlayerOptions: videoModel.videoPlayerOptions,
  );
}