initialize method

Future<YTMusic> initialize({
  1. String? cookies,
  2. String? gl,
  3. String? hl,
})

Initializes the YTMusic instance with provided cookies, geolocation, and language.

Implementation

Future<YTMusic> initialize({
  String? cookies,
  String? gl,
  String? hl,
}) async {
  if (hasInitialized) {
    return this;
  }
  if (cookies != null) {
    for (final cookieString in cookies.split("; ")) {
      final cookie = Cookie.fromSetCookieValue(cookieString);
      cookieJar.saveFromResponse(
        Uri.parse("https://www.youtube.com/"),
        [cookie],
      );
    }
  }

  await fetchConfig();

  if (gl != null) config['GL'] = gl;
  if (hl != null) config['HL'] = hl;

  hasInitialized = true;

  return this;
}