load method

  1. @override
void load(
  1. FlexPlayerSource source, {
  2. bool autoPlay = false,
  3. bool loop = false,
  4. bool mute = false,
  5. double volume = 1.0,
  6. double playbackSpeed = 1.0,
  7. Duration? position,
  8. VoidCallback? onInitialized,
})
override

Load the video player with the given source.

Implementation

@override
void load(
  FlexPlayerSource source, {
  bool autoPlay = false,
  bool loop = false,
  bool mute = false,
  double volume = 1.0,
  double playbackSpeed = 1.0,
  Duration? position,
  VoidCallback? onInitialized,
}) async {
  _initializationstream.add(InitializationEvent.initializing);
  try {
    _source = source;
    configuration = configuration.copyWith(
      autoPlay: autoPlay,
      loop: loop,
      volume: volume,
      playbackSpeed: playbackSpeed,
      position: position,
      isPlaying: autoPlay,
    );
    if (source is AssetFlexPlayerSource) {
      _videoPlayerController = VideoPlayerController.asset(source.asset);
    } else if (source is NetworkFlexPlayerSource) {
      if (source.url.endsWith('.m3u8')) {
        final response = await get(Uri.parse(source.url));
        String m3u8Content = response.body;
        // Extract stream qualities
        List<Map<String, String>> data = parseM3U8Content(m3u8Content);
        for (var element in data) {
          videosList.add(
            VideoData(
              url: element['url'] ?? "",
              quality: element['resolution'].toString().split("x").last,
            ),
          );
        }
        if (data.isEmpty) {
          videosList.add(VideoData(url: source.url, quality: 'Auto'));
        }
      } else {
        videosList.add(VideoData(url: source.url, quality: 'Auto'));
      }
      qualities.value = videosList.map((e) => e.quality).toSet().toList();
      qualities.sort((a, b) => a.compareTo(b));
      _videoPlayerController = VideoPlayerController.networkUrl(
        videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
        Uri.parse(videosList.first.url),
      );
    } else if (source is FileFlexPlayerSource) {
      _videoPlayerController = VideoPlayerController.file(source.file);
    } else if (source is YouTubeFlexPlayerSource) {
      final videoId = source.videoId;
      final flexYoutubecontroller = FlexYoutubeController.instance;
      await flexYoutubecontroller
          .getInitialUrl(videoId, isLive: source.isLive)
          .then(
        (value) {
          qualities.value = flexYoutubecontroller.videosList
              .map((e) => e.quality)
              .toSet()
              .toList();
          final streamInfo = flexYoutubecontroller.videosList.first;
          selectedQuality = flexYoutubecontroller.videosList.first.quality;
          _videoPlayerController = VideoPlayerController.networkUrl(
            Uri.parse(streamInfo.url.toString()),
          );
        },
      );
      flexYoutubecontroller.getVideoInfo(videoId);
      worker = ever(FlexYoutubeController.instance.videosList, (value) {
        videosList.value = value;
        qualities.value = value.map((e) => e.quality).toSet().toList();
      });
    }
    await _videoPlayerController.initialize().then((_) async {
      _startListeners();
      if (onInitialized != null) {
        onInitialized();
      }
      _videoPlayerController.setVolume(volume);
      _videoPlayerController.setPlaybackSpeed(playbackSpeed);
      _videoPlayerController.setLooping(loop);
      if (mute) {
        _videoPlayerController.setVolume(0);
      }
      if (position != null) {
        await _videoPlayerController.seekTo(position);
      }
      if (autoPlay) {
        _videoPlayerController.play();
      }
    });
  } catch (e) {
    log(e.toString());
    _initializationstream.add(InitializationEvent.uninitialized);
  }
}