init method

Future<bool> init({
  1. ZegoPIPConfig config = const ZegoPIPConfig(),
  2. ZegoPIPExpressConfig? expressConfig,
})

Initialize the PIP functionality.

This method must be called before using any other PIP features. It sets up the ZEGO Express engine and configures the PIP system.

config - PIP configuration including aspect ratio and other settings. expressConfig - ZEGO Express engine configuration. If null, it means the client has already created the express engine; If not null, zego_pip will internally create the express engine and register related express events. The expressConfig contains create, room, and event configurations. Please ensure that enablePlatformView is set to true

Returns true if initialization is successful, false otherwise.

Throws:

  • PlatformException if platform-specific initialization fails

Implementation

Future<bool> init({
  ZegoPIPConfig config = const ZegoPIPConfig(),

  /// If null, it means the client has already created the express engine;
  /// If not null, zego_pip will internally create the express engine and register related express events.
  ZegoPIPExpressConfig? expressConfig,
}) async {
  if (private.isInit) {
    return true;
  }

  private.isInit = true;
  private.pipConfig = config;
  private.expressConfig = expressConfig;
  private.event = expressConfig?.event ?? ZegoPIPExpressEvent();

  if (null != expressConfig?.create) {
    ZegoExpressEngine.onEngineStateUpdate = private.onEngineStateUpdate;
    ZegoExpressEngine.onDebugError = private.onDebugError;
    ZegoExpressEngine.onRoomStreamUpdate = private.onRoomStreamUpdate;
    ZegoExpressEngine.onRoomStateChanged = private.onRoomStateChanged;
    ZegoExpressEngine.onPlayerStateUpdate = private.onPlayerStateUpdate;
    ZegoExpressEngine.onRemoteCameraStateUpdate =
        private.onRemoteCameraStateUpdate;
    ZegoExpressEngine.onRemoteMicStateUpdate = private.onRemoteMicStateUpdate;

    await ZegoExpressEngine.createEngineWithProfile(
      ZegoEngineProfile(
        expressConfig!.create!.appID,
        expressConfig.create!.scenario,
        appSign: expressConfig.create!.appSign,
        enablePlatformView: true,
      ),
    );
  } else {
    /// External creation is responsible, assume it's already started
    private.engineStateNotifier.value = ZegoEngineState.Start;

    private.event?.onEngineStateUpdate = private.onEngineStateUpdate;
    private.event?.onDebugError = private.onDebugError;
    private.event?.onRoomStreamUpdate = private.onRoomStreamUpdate;
    private.event?.onRoomStateChanged = private.onRoomStateChanged;
    private.event?.onPlayerStateUpdate = private.onPlayerStateUpdate;
    private.event?.onRemoteCameraStateUpdate =
        private.onRemoteCameraStateUpdate;
    private.event?.onRemoteMicStateUpdate = private.onRemoteMicStateUpdate;
  }

  if (Platform.isIOS) {
    /// For detailed information, refer to https://doc-zh.zego.im/article/3673
    await ZegoPipPlatform.instance.enableCustomVideoRender(true);
  }

  /// Enable PIP when app goes to background by default
  final status = await private.enableWhenBackground();
  debugPrint('pip status:$status');

  if (expressConfig?.room?.canLoginRoom ?? false) {
    final result = await ZegoExpressEngine.instance.loginRoom(
      expressConfig!.room!.roomID,
      ZegoUser(expressConfig.room!.userID, expressConfig.room!.userName),
      config: ZegoRoomConfig(0, true, ''),
    );
    if (0 != result.errorCode) {
      return false;
    }

    private.isLoginRoom = true;
  }

  return true;
}