प्रयोगात्मक YouTube Playables Flutter Web SDK

इस गाइड में, Flutter वेब ऐप्लिकेशन के साथ YouTube Playables SDK टूल का इस्तेमाल करने का तरीका बताया गया है.

Flutter कॉन्फ़िगरेशन

डिफ़ॉल्ट Flutter वेब ऐप्लिकेशन को Playable के तौर पर काम करने के लिए, कुछ बदलाव करने होंगे. Flutter के 3.24.5 वर्शन के बाद, ये बदलाव करना ज़रूरी है.

डिफ़ॉल्ट रूप से, Flutter को रूट डायरेक्ट्री से संसाधन लोड करने के लिए कॉन्फ़िगर किया गया है. वहीं, Playables के लिए ज़रूरी है कि संसाधन, एंट्री पॉइंट के हिसाब से लोड किए जाएं. यह 404 गड़बड़ी के तौर पर दिखेगा या हो सकता है कि यह JavaScript कंसोल में दिखने वाली गड़बड़ी के तौर पर दिखे. यह गड़बड़ी इस तरह दिखती है: Refused to execute script from... because its MIME type ('text/html') is not executable. इसे फिर से कॉन्फ़िगर करने का एक तरीका यह है कि index.html फ़ाइल से यह टैग हटाया जाए:

<base href="$FLUTTER_BASE_HREF">

डिफ़ॉल्ट रूप से, Flutter कुछ लाइब्रेरी को ऐप्लिकेशन में एम्बेड करने के बजाय, डाइनैमिक तौर पर लोड करता है. CanvasKit इसका एक उदाहरण है. यह गड़बड़ी, JavaScript कंसोल में इस तरह दिखेगी: Refused to connect to '...' because it violates the following Content Security Policy directive: "connect-src 'self' blob: data:". लोड न होने पर, Flutter अन्य लॉग भी दिखा सकता है, जैसे कि Failed to download any of the following CanvasKit URLs. इसे ठीक करने के लिए, अपने वेब ऐप्लिकेशन को इस अतिरिक्त फ़्लैग के साथ बनाया जा सकता है:

$ flutter build web --no-web-resources-cdn

डिफ़ॉल्ट रूप से, Flutter वेब ऐप्लिकेशन, फ़ॉन्ट को ऐप्लिकेशन में एम्बेड करने के बजाय, डाइनैमिक तौर पर लोड करते हैं. डिफ़ॉल्ट Flutter ऐप्लिकेशन, Roboto फ़ॉन्ट का इस्तेमाल करता है. यह गड़बड़ी, JavaScript कंसोल में इस तरह दिखेगी: Refused to connect to '...' because it violates the following Content Security Policy directive: "connect-src 'self' blob: data". इसे ठीक करने के लिए, यह तरीका अपनाएं:

  • अपने फ़्लटर वेब ऐप्लिकेशन के रूट में "fonts" नाम का फ़ोल्डर बनाएं
  • fonts.google.com से Roboto फ़ॉन्ट फ़ैमिली डाउनलोड करें.
  • फ़ॉन्ट निकालें और Roboto-Regular.ttf को फ़ॉन्ट फ़ोल्डर में कॉपी करें
  • अपने फ़्लटर वेब ऐप्लिकेशन के pubspec.yaml में यह लाइन जोड़ें:
  fonts:
    - family: Roboto
      fonts:
       - asset: fonts/Roboto-Regular.ttf

फ़ॉन्ट कॉन्फ़िगरेशन के बारे में ज़्यादा जानकारी, Flutter के दस्तावेज़ में मिल सकती है.

SDK टूल इंटिग्रेशन

YouTube Playables SDK टूल का इस्तेमाल, Flutter वेब गेम में किया जा सकता है. इसके लिए, इस तरह के JS-इंटरऑप रैपर का इस्तेमाल करें:

ytgame.dart

// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@JS()
library ytgame_api;

import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;

enum PlayablesErrorType {
  unknown('UNKNOWN'),
  apiUnavailable('API_UNAVAILABLE'),
  invalidParams('INVALID_PARAMS'),
  sizeLimitExceeded('SIZE_LIMIT_EXCEEDED');

  const PlayablesErrorType(String type) : _type = type;
  final String _type;

  @override
  String toString() => _type;

  static PlayablesErrorType fromString(String errorType) {
    return values.firstWhere(
      (element) => element._type == errorType,
    );
  }
}

@JS()
@staticInterop
class PlayablesError {}

extension PlayablesErrorExtension on PlayablesError {
  @JS('errorType')
  external String get _errorType;
  PlayablesErrorType get errorType {
    return PlayablesErrorType.fromString(_errorType);
  }
}

@JS()
@anonymous
@staticInterop
class PlayablesScore {
  external factory PlayablesScore({
    required num value,
  });
}

extension PlayablesScoreExtension on PlayablesScore {
  external num get value;
}

// engagement
@JS()
@staticInterop
class PlayablesEngagement {}

extension PlayablesEngagementExtension on PlayablesEngagement {
  @JS('sendScore')
  external Object _sendScore(PlayablesScore score);
  Future<void> sendScore(PlayablesScore score) {
    return js_util.promiseToFuture(_sendScore(score));
  }
}

// game
@JS()
@staticInterop
class PlayablesGame {}

extension PlayablesGameExtension on PlayablesGame {
  external void firstFrameReady();
  external void gameReady();

  @JS('loadData')
  external Object _loadData();
  Future<String?> loadData() {
    return js_util.promiseToFuture<String?>(_loadData());
  }

  @JS('saveData')
  external Object _saveData(String? data);
  Future<void> saveData(String? data) {
    return js_util.promiseToFuture<void>(_saveData(data));
  }
}

// health
@JS()
@staticInterop
class PlayablesHealth {}

extension PlayablesHealthExtension on PlayablesHealth {
  external void logError();
  external void logWarning();
}

// system
typedef PlayablesUnsetCallback = void Function();
typedef PlayablesOnAudioEnabledChange = void Function(bool isAudioEnabled);
typedef PlayablesOnPause = void Function();
typedef PlayablesOnResume = void Function();

@JS()
@staticInterop
class PlayablesSystem {}

extension PlayablesSystemExtension on PlayablesSystem {
  external bool isAudioEnabled();

  @JS('onAudioEnabledChange')
  external PlayablesUnsetCallback _onAudioEnabledChange(
      PlayablesOnAudioEnabledChange callback);
  PlayablesUnsetCallback onAudioEnabledChange(
      PlayablesOnAudioEnabledChange callback) {
    return _onAudioEnabledChange(allowInterop(callback));
  }

  @JS('onPause')
  external PlayablesUnsetCallback _onPause(PlayablesOnPause callback);
  PlayablesUnsetCallback onPause(PlayablesOnPause callback) {
    return _onPause(allowInterop(callback));
  }

  @JS('onResume')
  external PlayablesUnsetCallback _onResume(PlayablesOnResume callback);
  PlayablesUnsetCallback onResume(PlayablesOnResume callback) {
    return _onResume(allowInterop(callback));
  }
}

@JS()
@staticInterop
class Playables {}

extension PlayablesExtension on Playables {
  @JS('SDK_VERSION')
  external String get sdkVersion;
  external PlayablesEngagement get engagement;
  external PlayablesGame get game;
  external PlayablesHealth get health;
  external PlayablesSystem get system;
}

@JS('ytgame')
external Playables get ytgame;

इस्तेमाल

  1. वेब SDK टूल को सेटअप और शुरू करने के लिए, निर्देशों का पालन करें.
    • ज़रूरी स्क्रिप्ट टैग शामिल करने के लिए, अपने Flutter वेब ऐप्लिकेशन के web/index.html में बदलाव करें.
  2. अपने Flutter वेब ऐप्लिकेशन के src में ytgame.dart की कॉपी जोड़ें.
  3. पक्का करें कि Flutter में js पैकेज जोड़ा गया हो. इसके लिए, flutter pub add js कमांड चलाएं.
  4. ज़रूरत के हिसाब से import 'src/location/of/ytgame.dart'; जोड़ें.
  5. SDK टूल को ऐक्सेस करने के लिए, ytgame ऑब्जेक्ट का इस्तेमाल करें.

उदाहरण

यहां Dart API के इस्तेमाल के कुछ उदाहरण दिए गए हैं.

firstFrameReady और gameReady

आपका गेम सही तरीके से शुरू हो, इसके लिए आपको firstFrameReadyपहला फ़्रेम दिखाने के लिए और gameReadyगेम के साथ इंटरैक्ट करने के लिए कॉल करना होगा. डिफ़ॉल्ट Flutter ऐप्लिकेशन में ऐसा करने का एक तरीका यह है कि main.dart फ़ाइल में कॉल जोड़ें:

...
import 'src/ytgame.dart';
...

void main() {
  ytgame.game.firstFrameReady();
  ytgame.game.gameReady();
  runApp(const MyApp());
}

ध्यान दें कि यह सिर्फ़ एक उदाहरण है, ताकि आपका गेम चल सके. इसे सही तरीके से लागू करने के लिए, आपको इन कॉल की जगह को सही तरीके से तय करना होगा.

sendScore

इस उदाहरण में, Dart में sendScore(int points) को लागू करने का तरीका बताया गया है:

...
import 'src/ytgame.dart';
...

/// Sends [points] as score to YT Playables
Future<void> sendScore(int points) async {
  // Create a score object...
  final PlayablesScore score = PlayablesScore(
    value: points,
  );
  // Submit the score to ytgame
  await ytgame.engagement.sendScore(score);
}

onPause

इस उदाहरण में बताया गया है कि ज़रूरत पड़ने पर अपने इंजन को रोकने के लिए, कोई गेम YT Playables से आने वाले Pause इवेंट को कैसे सुन सकता है:

...
import 'src/ytgame.dart';
...

/// The instance of your game.
late Game myGame;

/// Callback to stop listening for Pause events (cleanup).
PlayablesUnsetCallback? _unsetOnPause;
...

/// Sets a [callback] to respond to Pause events.
void registerOnPauseListener(PlayablesOnPause callback) {
  _unsetOnPause = ytgame.system.onPause(callback);
}

/// Stops listening to Pause events, and clears [_unsetOnPause].
void unsetOnPauseListener() {
  if (_unsetOnPause != null) {
    _unsetOnPause!();
    _unsetOnPause = null;
  }
}

...

/// Initialization for your game
void initGame() {
  ...
  myGame = ...
  registerOnPauseListener(_onPause);
}

void _onPause() {
  // This is called when a Pause event is received from YT Playables.
  myGame.pauseEngine();
}

/// Teardown for your game
void disposeGame() {
  ...
  unsetOnPauseListener();
}

YT Playables API का पूरा रेफ़रंस देखें.