startDownload method

  1. @override
Future<String?> startDownload(
  1. String url
)
override

Starts downloading a video from the given url. Returns a unique identifier for the download task.

Implementation

@override
Future<String?> startDownload(String url) async {
  try {
    // Clear previous progress values for this URL
    _lastReportedProgress[url] = 0.0;
    _lastReportedInfo.remove(url);

    // Initialize the progress stream controller if it doesn't exist
    _getOrCreateProgressController(url);
    _getOrCreateInfoController(url);

    // Send initial progress of 0.0
    _progressStreamControllers[url]?.add(0.0);

    // Send initial info
    final initialInfo = DownloadProgress(
      url: url,
      progress: 0.0,
      bytesDownloaded: 0,
    );
    _infoStreamControllers[url]?.add(initialInfo);

    final result = await methodChannel.invokeMethod<String>('startDownload', {
      'url': url,
    });

    return result;
  } catch (e) {
    debugPrint('Error starting download: $e');
    return null;
  }
}