onInit method

  1. @override
void onInit()
override

Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.

Implementation

@override
void onInit() async {
  log("init state called");
  // videoDetails.value = Get.arguments;
  // log(videoDetails.value.title!);
  // print(videoDetails.value.link);
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  manifest =
      await yt.videos.streamsClient.getManifest(getYouTubeVideoId(link));
  video = await yt.videos.get(getYouTubeVideoId(link));
  qualities.value = manifest!.muxed.toList();
  if (manifest!.muxed.toList().first.qualityLabel == '144p') {
    qualities.remove(manifest!.muxed.toList().first);
  }
  MuxedStreamInfo streamInfo = manifest!.muxed.bestQuality;
  var url = streamInfo.url;
  controller = VideoPlayerController.networkUrl(
    url,
    videoPlayerOptions: VideoPlayerOptions(),
  )..initialize().then((value) {
      position.value = controller.value.position;
      duration.value = controller.value.duration;
      isInitialized.value = true;
      controller.play();
      isPlaying.value = true;
    });
  controller.addListener(() async {
    position.value = controller.value.position;
    sliderVal.value = position.value.inSeconds / duration.value.inSeconds;
  });

  controller.setLooping(true);
  tabController = TabController(length: 2, vsync: this);
  ScreenBrightness()
      .current
      .then((brightness) => setBrightness.value = brightness);

  setVolumeValue.value = controller.value.volume;
  setVolumeValue.value == 0 ? isMute.value = true : isMute.value = false;
  controller.addListener(() {
    if (caption.isNotEmpty) {
      currentSubtitle = getSubtitleForCurrentPosition(position.value);
      // print("---->${currentSubtitle!.data}");
    }
  });
  super.onInit();
}