openharmony 鸿蒙 js-apis-pipWindow

2025-06-12 浏览 (1)

@ohos.PiPWindow (PiP Window)

The PiPWindow module provides basic APIs for manipulating Picture in Picture (PiP). For example, you can use the APIs to check whether the PiP feature is supported and create a PiP controller to start or stop a PiP window. PiP is mainly used in video playback, video calls, or video meetings.

NOTE

The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This module must be used on the device that supports the SystemCapability.Window.SessionManager capability. For details, see SystemCapability.

Modules to Import

import { PiPWindow } from '@kit.ArkUI';

PiPWindow.isPiPEnabled

isPiPEnabled(): boolean

Checks whether the PiP feature is supported.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Return value

TypeDescription
booleanCheck result. The value true means that the PiP feature is supported, and false means the opposite.

Example

let enable: boolean = PiPWindow.isPiPEnabled();
console.info('isPipEnabled:' + enable);

PiPWindow.create

create(config: PiPConfiguration): Promise<PiPController>

Creates a PiP controller. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
configPiPConfigurationYesOptions for creating the PiP controller. This parameter cannot be empty, and context and componentController that are used to construct this parameter cannot be empty. When constructing this parameter, templateType (if specified) must be a value defined in PiPTemplateType, and controlGroups (if specified) must match the value of templateType. For details, see PiPControlGroup.

Return value

TypeDescription
Promise<PiPController>Promise used to return the PiP controller.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
801Capability not supported.Failed to call the API due to limited device capabilities.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { BuilderNode, FrameNode, NodeController, UIContext } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

class Params {
  text: string = '';
  constructor(text: string) {
    this.text = text;
  }
}

// You can use the @Builder decorator to construct layouts.
@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text)
      .fontSize(20)
      .fontColor(Color.Red)
  }
  .width('100%') // The PiP window is displayed at full size in the width direction.
  .height('100%') // The PiP window is displayed at full size in the height direction.
}

// You can extend NodeController to customize the UI controller.
class TextNodeController extends NodeController {
  private message: string;
  private textNode: BuilderNode<[Params]>|null = null;
  constructor(message: string) {
    super();
    this.message = message;
  }

  // Use BuilderNode to load the custom layouts.
  makeNode(context: UIContext): FrameNode|null {
    this.textNode = new BuilderNode(context);
    this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
    return this.textNode.getFrameNode();
  }

  // You can customize this method to update layouts.
  update(message: string) {
    console.log(`update message: ${message}`);
    if (this.textNode !== null) {
      this.textNode.update(new Params(message));
    }
  }
}

let pipController: PiPWindow.PiPController|undefined = undefined;
let mXComponentController: XComponentController = new XComponentController(); // Use the mXComponentController to initialize the XComponent: XComponent( {id: 'video', type: 'surface', controller: mXComponentController} ). This ensures that the XComponent content can be migrated to the PiP window.
let nodeController: TextNodeController = new TextNodeController('this is custom UI');
let navId: string = "page_1"; // The navigation ID of the current page is page_1. For details, see the definition of PiPConfiguration. The navigation name is customized.
let contentWidth: number = 800; // The content width is 800 px.
let contentHeight: number = 600; // The content height is 600 px.
let para: Record<string, number> = { 'PropA': 47 };
let localStorage: LocalStorage = new LocalStorage(para);
let res: boolean = localStorage.setOrCreate('PropB', 121);
let ctx = this.getUIContext().getHostContext() as common.UIAbilityContext; // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
let defaultWindowSize: number = 1; // Set the window to be a small window when first pulled up in PiP.
let config: PiPWindow.PiPConfiguration = {
  context: ctx,
  componentController: mXComponentController,
  navigationId: navId,
  templateType: PiPWindow.PiPTemplateType.VIDEO_PLAY,
  contentWidth: contentWidth,
  contentHeight: contentHeight,
  controlGroups: [PiPWindow.VideoPlayControlGroup.VIDEO_PREVIOUS_NEXT],
  customUIController: nodeController, // Optional. Set this parameter if you want to display the custom UI at the top of the PiP window.
  localStorage: localStorage, // Optional. Set this parameter if you want to track the main window instance.
  defaultWindowSizeType: defaultWindowSize // Optional. If you need to configure the default window size upon launch, set this parameter.
};

let promise: Promise<PiPWindow.PiPController> = PiPWindow.create(config);
promise.then((data: PiPWindow.PiPController) => {
  pipController = data;
  console.info(`Succeeded in creating pip controller. Data:${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to create pip controller. Cause:${err.code}, message:${err.message}`);
});

PiPWindow.create12+

create(config: PiPConfiguration, contentNode: typeNode.XComponent): Promise<PiPController>

Creates a PiP controller through a type node. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
configPiPConfigurationYesOptions for creating the PiP controller. This parameter cannot be empty, and context that is used to construct this parameter cannot be empty. When constructing this parameter, templateType (if specified) must be a value defined in PiPTemplateType, and controlGroups (if specified) must match the value of templateType. For details, see PiPControlGroup.
contentNodetypeNode.XComponentYesContent to be rendered in the PiP window. The parameter value cannot be empty.

Return value

TypeDescription
Promise<PiPController>Promise used to return the PiP controller.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
801Capability not supported.Failed to call the API due to limited device capabilities.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { PiPWindow, typeNode, UIContext } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

let pipController: PiPWindow.PiPController|undefined = undefined;
let xComponentController: XComponentController = new XComponentController();
let ctx = this.getUIContext().getHostContext() as common.UIAbilityContext; // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
let options: XComponentOptions = {
  type: XComponentType.SURFACE,
  controller: xComponentController
}
let xComponent = typeNode.createNode(this.getUIContext(), 'XComponent', options);
let contentWidth: number = 800; // The content width is 800 px.
let contentHeight: number = 600; // The content height is 600 px.
let config: PiPWindow.PiPConfiguration = {
  context: ctx,
  componentController: xComponentController,
  templateType: PiPWindow.PiPTemplateType.VIDEO_PLAY,
  contentWidth: contentWidth,
  contentHeight: contentHeight
};

let promise: Promise<PiPWindow.PiPController> = PiPWindow.create(config, xComponent);
promise.then((data: PiPWindow.PiPController) => {
  pipController = data;
  console.info(`Succeeded in creating pip controller. Data:${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to create pip controller. Cause:${err.code}, message:${err.message}`);
});

PiPConfiguration

Defines the parameters for creating a PiP controller.

System capability: SystemCapability.Window.SessionManager

NameTypeMandatoryDescription
contextBaseContextYesContext environment.
Atomic service API: This API can be used in atomic services since API version 12.
componentControllerXComponentControllerYesOriginal XComponent controller.
Atomic service API: This API can be used in atomic services since API version 12.
navigationIdstringNoNavigation ID of the current page. If no value is passed, the page does not need to be cached.
1. When the UIAbility uses Navigation to manage pages, set the ID of the Navigation component for the PiP controller. This ensures that the original page can be restored from the PiP window.
2. When the UIAbility uses Router to manage pages, you do not need to set the ID of the Navigation component for the PiP controller.
3. If the UIAbility has only one page, you do not need to set the navigation ID. The original page can be restored from the PiP window.
Atomic service API: This API can be used in atomic services since API version 12.
templateTypePiPTemplateTypeNoTemplate type, which is used to distinguish video playback, video call, and video meeting scenarios. If no value is passed, the video playback template is used.
Atomic service API: This API can be used in atomic services since API version 12.
contentWidthnumberNoWidth of the original content, in px. It is used to determine the aspect ratio of the PiP window. When the PiP controller is created in typeNode mode, the default value is 1920. When the PiP controller is created not in typeNode mode, the default value is the width of the XComponent.
Atomic service API: This API can be used in atomic services since API version 12.
contentHeightnumberNoHeight of the original content, in px. It is used to determine the aspect ratio of the PiP window. When the PiP controller is created in typeNode mode, the default value is 1080. When the PiP controller is created not in typeNode mode, the default value is the height of the XComponent.
Atomic service API: This API can be used in atomic services since API version 12.
controlGroups12+Array<PiPControlGroup>NoA list of optional component groups of the PiP controller. An application can configure whether to display these optional components. If this parameter is not set for an application, the basic components (for example, play/pause of the video playback component group) are displayed. A maximum of three components can be configured in the list.
Atomic service API: This API can be used in atomic services since API version 12.
customUIController12+NodeControllerNoCustom UI that can be displayed at the top of the PiP window. If no value is passed, custom UI is not used.
Atomic service API: This API can be used in atomic services since API version 12.
localStorage17+LocalStorageNoA page-level UI state storage unit. In multi-instance scenarios, it can be used to track the UI state storage object of the main window instance. If no value is passed, you cannot retrieve the main window's UI storage object through the PiP window.
Atomic service API: This API can be used in atomic services since API version 17.
defaultWindowSizeType19+numberNoSize of the window when first pulled up in PiP.
The value 0 means that no size is set, and the window will launch at the size it was before being closed in the previous PiP session.
The value 1 means a small window, and 2 means a large window.
If no value is passed, 0 is used.
Atomic service API: This API can be used in atomic services since API version 19.

PiPWindowSize15+

Describes the size of a PiP window.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

NameTypeReadableWritableDescription
widthnumberYesNoWindow width, in px. The value must be a positive integer and cannot be greater than the screen width.
heightnumberYesNoWindow height, in px. The value must be a positive integer and cannot be greater than the screen height.
scalenumberYesNoScale factor of the window, representing the display size relative to the width and height. The value is a floating point number in the range (0.0, 1.0]. The value 1 means that the window matches the specified width and height.

PiPWindowInfo15+

Describes the PiP window information.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

NameTypeReadableWritableDescription
windowIdnumberYesNoID of the PiP window.
sizePiPWindowSizeYesNoSize of the PiP window.

PiPTemplateType

Enumerates the PIP template types.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
VIDEO_PLAY0Video playback template. A PiP window will be started during video playback, and the video playback template is loaded.
VIDEO_CALL1Video call template. A PiP window will be started during a video call, and the video call template will be loaded.
VIDEO_MEETING2Video meeting template. A PiP window will be started during a video meeting, and the video meeting template will be loaded.
VIDEO_LIVE3Live template. A PiP window will be started during a live, and the live template is loaded.

PiPState

Enumerates the PiP states.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
ABOUT_TO_START1PiP is about to start.
STARTED2PiP is started.
ABOUT_TO_STOP3PiP is about to stop.
STOPPED4PiP is stopped.
ABOUT_TO_RESTORE5The original page is about to restore.
ERROR6An error occurs during the execution of the PiP lifecycle.

PiPControlGroup12+

type PiPControlGroup = VideoPlayControlGroup|VideoCallControlGroup|VideoMeetingControlGroup|VideoLiveControlGroup

Describes the optional component groups of the PiP controller. An application can configure whether to display these optional components. By default, the controller displays only basic components (such as the play/pause component of the video playback component group).

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
VideoPlayControlGroupVideo playback component group.
VideoCallControlGroupVideo call component group.
VideoMeetingControlGroupVideo meeting component group.
VideoLiveControlGroupLive video component group.

VideoPlayControlGroup12+

Enumerates the video playback component groups. They are used only when PiPTemplateType is set to VIDEO_PLAY.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
VIDEO_PREVIOUS_NEXT101Previous/Next component group for video playback.
This component group is mutually exclusive with the fast-forward/rewind component group. It cannot be added if the fast-forward/rewind component group is added.
FAST_FORWARD_BACKWARD102Fast-forward/Rewind component group for video playback.
This component group is mutually exclusive with the previous/next component group. It cannot be added if the previous/next component group is added.

VideoCallControlGroup12+

Enumerates the video call component groups. They are used only when PiPTemplateType is set to VIDEO_CALL.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
MICROPHONE_SWITCH201Microphone on/off component group.
HANG_UP_BUTTON202Hang-up component group.
CAMERA_SWITCH203Camera on/off component group.
MUTE_SWITCH204Mute/Unmute component group.

VideoMeetingControlGroup12+

Enumerates the video meeting component groups. They are used only when PiPTemplateType is set to VIDEO_MEETING.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
HANG_UP_BUTTON301Hang-up component group.
CAMERA_SWITCH302Camera on/off component group.
MUTE_SWITCH303Mute/Unmute component group.
MICROPHONE_SWITCH304Microphone on/off component group.

VideoLiveControlGroup12+

Enumerates the live video component groups. They are used only when PiPTemplateType is set to VIDEO_LIVE.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
VIDEO_PLAY_PAUSE401Play/Pause component group for live video.
MUTE_SWITCH402Mute/Unmute component group.

PiPActionEventType

type PiPActionEventType = PiPVideoActionEvent|PiPCallActionEvent|PiPMeetingActionEvent|PiPLiveActionEvent

Enumerates the types of action events of the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
PiPVideoActionEventAction event for components displayed on the video playback controller.
PiPCallActionEventAction event for components displayed on the video call controller.
PiPMeetingActionEventAction event for components displayed on the video meeting controller.
PiPLiveActionEventAction event for components displayed on the live video controller.

PiPVideoActionEvent

type PiPVideoActionEvent = 'playbackStateChanged'|'nextVideo'|'previousVideo'|'fastForward'|'fastBackward'

Defines the PiP action event during video playback.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
'playbackStateChanged'The playback status changes.
'nextVideo'Plays the next video.
'previousVideo'Plays the previous video.
'fastForward'12+Fast forwards the video. This value is supported since API version 12.
'fastBackward'12+Rewinds the video. This value is supported since API version 12.

PiPCallActionEvent

type PiPCallActionEvent = 'hangUp'|'micStateChanged'|'videoStateChanged'|'voiceStateChanged'

Defines the PiP action event in a video call.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
'hangUp'The video call is hung up.
'micStateChanged'The microphone is muted or unmuted.
'videoStateChanged'The camera is turned on or off.
'voiceStateChanged'12+The speaker is muted or unmuted.

PiPMeetingActionEvent

type PiPMeetingActionEvent = 'hangUp'|'voiceStateChanged'|'videoStateChanged'|'micStateChanged'

Defines the PiP action event in a video meeting.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
'hangUp'The video meeting is hung up.
'voiceStateChanged'The speaker is muted or unmuted.
'videoStateChanged'The camera is turned on or off.
'micStateChanged'12+The microphone is muted or unmuted.

PiPLiveActionEvent

type PiPLiveActionEvent = 'playbackStateChanged'|'voiceStateChanged'

Defines the PiP action event in a live.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

TypeDescription
'playbackStateChanged'The live is played or paused.
'voiceStateChanged'12+The speaker is muted or unmuted.

PiPControlStatus12+

Enumerates the statuses of components displayed on the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
PLAY1Play.
PAUSE0Pause.
OPEN1Open.
CLOSE0Close.

PiPControlType12+

Enumerates the types of components displayed on the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameValueDescription
VIDEO_PLAY_PAUSE0Play/Pause component.
VIDEO_PREVIOUS1Previous component in video scenarios.
VIDEO_NEXT2Next component in video scenarios.
FAST_FORWARD3Fast-forward component in video scenarios.
FAST_BACKWARD4Rewind component in video scenarios.
HANG_UP_BUTTON5Hang-up component.
MICROPHONE_SWITCH6Microphone on/off component.
CAMERA_SWITCH7Camera on/off component.
MUTE_SWITCH8Mute/Unmute component.

ControlPanelActionEventCallback12+

type ControlPanelActionEventCallback = (event: PiPActionEventType, status?: number) => void

Describes the action event callback of the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
eventPiPActionEventTypeYesType of the action event of the PiP controller.
The application performs processing based on the action event. For example, if the 'playbackStateChanged' event is triggered, the application starts or stops the video.
statusnumberNoStatus of a component that can be switched. For example, for a microphone on/off component group, a camera on/off component group, and a mute/unmute component group, the value 1 means that the component is enabled and 0 means that the component is disabled. For other components, the default value -1 is used.

ControlEventParam12+

Describes the parameters in the callback of the action event of the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

NameTypeMandatoryDescription
controlTypePiPControlTypeYesType of the action event of the PiP controller. The application performs processing based on the component type. For example, if the video play/pause component is touched, the application starts or stops the video.
statusPiPControlStatusNoStatus of a component that can be switched. For example, for a microphone on/off component group, a camera on/off component group, and a mute/unmute component group, the value PiPControlStatus.PLAY means that the component is enabled and PiPControlStatus.PAUSE means that the component is disabled. For the hang-up component, the default value is -1.

PiPController

Implements a PiP controller that starts, stops, or updates a PiP window and registers callbacks.

Before calling any of the following APIs, you must use PiPWindow.create() to create a PiPController instance.

System capability: SystemCapability.Window.SessionManager

startPiP

startPiP(): Promise<void>

Starts a PiP window. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Window Error Codes.

IDError Message
1300012The PiP window state is abnormal.
1300013Failed to create the PiP window.
1300014PiP internal error.
1300015Repeated PiP operation.

Example

let promise : Promise<void> = pipController.startPiP();
promise.then(() => {
  console.info(`Succeeded in starting pip.`);
}).catch((err: BusinessError) => {
  console.error(`Failed to start pip. Cause:${err.code}, message:${err.message}`);
});

stopPiP

stopPiP(): Promise<void>

Stops a PiP window. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Window Error Codes.

IDError Message
1300011Failed to destroy the PiP window.
1300012The PiP window state is abnormal.
1300015Repeated PiP operation.

Example

let promise : Promise<void> = pipController.stopPiP();
promise.then(() => {
  console.info(`Succeeded in stopping pip.`);
}).catch((err: BusinessError) => {
  console.error(`Failed to stop pip. Cause:${err.code}, message:${err.message}`);
});

setAutoStartEnabled

setAutoStartEnabled(enable: boolean): void

Sets whether to automatically start a PiP window when the user returns to the home screen. By default, no PiP window is started.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether to automatically start a PiP window when the user returns to the home screen. The value true means to automatically start a PiP window in such a case, and false means the opposite. If the automatic PiP startup feature is disabled in Settings, a PiP window will not be automatically started in such a case even if this parameter is set to true.

Example

let enable: boolean = true;
pipController.setAutoStartEnabled(enable);

updateContentSize

updateContentSize(width: number, height: number): void

Updates the media content size when the media content changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
widthnumberYesWidth of the media content, in px. The value must be a number greater than 0. It is used to update the aspect ratio of the PiP window.
heightnumberYesHeight of the media content, in px. The value must be a number greater than 0. It is used to update the aspect ratio of the PiP window.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.

Example

let width: number = 540; // The content width changes to 540 px.
let height: number = 960; // The content height changes to 960 px.
pipController.updateContentSize(width, height);

updatePiPControlStatus12+

updatePiPControlStatus(controlType: PiPControlType, status: PiPControlStatus): void

Updates the PiP component status.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
controlTypePiPControlTypeYesType of the component displayed on the PiP controller. Currently, only VIDEO_PLAY_PAUSE, MICROPHONE_SWITCH, CAMERA_SWITCH, and MUTE_SWITCH are supported.
statusPiPControlStatusYesStatus of the component displayed on the PiP controller.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed

Example

let controlType: PiPWindow.PiPControlType = PiPWindow.PiPControlType.VIDEO_PLAY_PAUSE; // Play/Pause component displayed on the video playback control panel.
let status: PiPWindow.PiPControlStatus = PiPWindow.PiPControlStatus.PLAY; // The Play/Pause component displayed on the video playback control panel is in the playing state.
pipController.updatePiPControlStatus(controlType, status);

updateContentNode18+

updateContentNode(contentNode: typeNode.XComponent): Promise<void>

Updates the PiP node content.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
contentNodetypeNode.XComponentYesContent to be rendered in the PiP window. The parameter value cannot be empty.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed
801Capability not supported.Failed to call the API due to limited device capabilities.
1300014PiP internal error.

Example

import { typeNode, UIContext } from '@kit.ArkUI';

let context: UIContext|undefined = undefined; // You can pass UIContext or use this.getUIContext() in the layout to assign a valid value to context.

try {
  let contentNode = typeNode.createNode(context, "XComponent");
  pipController.updateContentNode(contentNode);
} catch (exception) {
  console.error(`Failed to update content node. Cause: ${exception.code}, message: ${exception.message}`);
}

setPiPControlEnabled12+

setPiPControlEnabled(controlType: PiPControlType, enabled: boolean): void

Sets the enabled status for a component displayed on the PiP controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
controlTypePiPControlTypeYesType of the component displayed on the PiP controller.
enabledbooleanYesEnabled status of the component displayed on the PiP controller. The value true means that the component is enabled, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Params error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed

Example

let controlType: PiPWindow.PiPControlType = PiPWindow.PiPControlType.VIDEO_PLAY_PAUSE; // Play/Pause component displayed on the video playback control panel.
let enabled: boolean = false; // The Play/Pause component displayed on the video playback control panel is in the disabled state.
pipController.setPiPControlEnabled(controlType, enabled);

getPiPWindowInfo15+

getPiPWindowInfo(): Promise<PiPWindowInfo>

Obtains the PIP window information.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

Return value

TypeDescription
Promise<PiPWindowInfo>Promise used to return the information about the current PIP window.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
801Capability not supported. Failed to call the API due to limited device capabilities.
1300014PiP internal error.

Example

let pipWindowInfo: PiPWindow.PiPWindowInfo|undefined = undefined;
try {
  let promise : Promise<PiPWindow.PiPWindowInfo> = pipController.getPiPWindowInfo();
  promise.then((data) => {
    pipWindowInfo = data;
    console.info('Success in get pip window info. Info: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.error(`Failed to get pip window info. Cause code: ${err.code}, message: ${err.message}`);
  });
} catch (exception) {
  console.error(`Failed to get pip window info. Cause code: ${exception.code}, message: ${exception.message}`);
}

on('stateChange')

on(type: 'stateChange', callback: (state: PiPState, reason: string) => void): void

Subscribes to PiP state events. To avoid potential memory leaks, you are advised to stop listening when it is no longer needed.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'stateChange' is triggered when the PiP state changes.
callbackfunctionYesCallback used to return the result, which includes the following information:
- state: PiPState, indicating the new PiP state.
- reason: a string indicating the reason for the state change.

Example

pipController.on('stateChange', (state: PiPWindow.PiPState, reason: string) => {
  let curState: string = '';
  switch (state) {
    case PiPWindow.PiPState.ABOUT_TO_START:
      curState = 'ABOUT_TO_START';
      break;
    case PiPWindow.PiPState.STARTED:
      curState = 'STARTED';
      break;
    case PiPWindow.PiPState.ABOUT_TO_STOP:
      curState = 'ABOUT_TO_STOP';
      break;
    case PiPWindow.PiPState.STOPPED:
      curState = 'STOPPED';
      break;
    case PiPWindow.PiPState.ABOUT_TO_RESTORE:  
      curState = 'ABOUT_TO_RESTORE';
      break;
    case PiPWindow.PiPState.ERROR:
      curState = 'ERROR';
      break;
    default:
      break;
  }
  console.info('stateChange:' + curState + ' reason:' + reason);
});

off('stateChange')

off(type: 'stateChange'): void

Unsubscribes from PiP state events.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'stateChange' is triggered when the PiP state changes.

Example

pipController.off('stateChange');

on('controlPanelActionEvent')

on(type: 'controlPanelActionEvent', callback: ControlPanelActionEventCallback): void

Subscribes to PiP action events. To avoid potential memory leaks, you are advised to stop listening when it is no longer needed. The on('controlEvent') API is preferred.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'controlPanelActionEvent' indicates the PiP action event.
callbackControlPanelActionEventCallbackYesAction event callback of the PiP controller.

Example

pipController.on('controlPanelActionEvent', (event: PiPWindow.PiPActionEventType, status?: number) => {
  switch (event) {
    case 'playbackStateChanged':
      if (status === 0) {
        // Stop the video.
      } else if (status === 1) {
        // Play the video.
      }
      break;
    case 'nextVideo':
      // Switch to the next video.
      break;
    case 'previousVideo':
      // Switch to the previous video.
      break;
    case 'fastForward':
      // Fast forward the video.
      break;
    case 'fastBackward':
      // Rewind the video.
      break;
    default:
      break;
  }
  console.info('registerActionEventCallback, event:' + event);
});

on('controlEvent')12+

on(type: 'controlEvent', callback: Callback<ControlEventParam>): void

Subscribes to PiP action events. To avoid potential memory leaks, you are advised to stop listening when it is no longer needed.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'controlEvent' indicates the PiP action event.
callbackCallback<ControlEventParam>YesAction event callback of the PiP controller.

Example

pipController.on('controlEvent', (control) => {
  switch (control.controlType) {
    case PiPWindow.PiPControlType.VIDEO_PLAY_PAUSE:
      if (control.status === PiPWindow.PiPControlStatus.PAUSE) {
        // Stop the video.
      } else if (control.status === PiPWindow.PiPControlStatus.PLAY) {
        // Play the video.
      }
      break;
    case PiPWindow.PiPControlType.VIDEO_NEXT:
      // Switch to the next video.
      break;
    case PiPWindow.PiPControlType.VIDEO_PREVIOUS:
      // Switch to the previous video.
      break;
    case PiPWindow.PiPControlType.FAST_FORWARD:
      // Fast forward the video.
      break;
    case PiPWindow.PiPControlType.FAST_BACKWARD:
      // Rewind the video.
      break;
    default:
      break;
  }
  console.info('registerControlEventCallback, controlType:' + control.controlType + ', status' + control.status);
});

off('controlPanelActionEvent')

off(type: 'controlPanelActionEvent'): void

Unsubscribes from PiP action events. The off('controlEvent') API is preferred.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'controlPanelActionEvent' indicates the PiP action event.

Example

pipController.off('controlPanelActionEvent');

off('controlEvent')12+

off(type: 'controlEvent', callback?: Callback<ControlEventParam>): void

Unsubscribes from PiP action events.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'controlEvent' indicates the PiP action event.
callbackCallback<ControlEventParam>NoAction event callback of the PiP controller. If no value is passed in, all subscriptions to the specified event are canceled.

Example

pipController.off('controlEvent', () => {});

on('pipWindowSizeChange')15+

on(type: 'pipWindowSizeChange', callback: Callback<PiPWindowSize>): void

Subscribes to PiP window size change events. To avoid potential memory leaks, you are advised to stop listening when it is no longer needed.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'pipWindowSizeChange', indicating the PiP window size change event.
callbackCallback<PiPWindowSize>YesCallback used to return the size of the current PiP window.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Params error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.Failed to call the API due to limited device capabilities.
1300014PiP internal error.

Example

try {
  pipController.on('pipWindowSizeChange', (size: PiPWindow.PiPWindowSize) => {
    console.info('Succeeded in enabling the listener for pip window size changes. size: ' + JSON.stringify(size));
  });
} catch (exception) {
  console.error(`Failed to enable the listener for pip window size changes. Cause code: ${exception.code}, message: ${exception.message}`);
}

off('pipWindowSizeChange')15+

off(type: 'pipWindowSizeChange', callback?: Callback<PiPWindowSize>): void

Unsubscribes from the PiP window size change event.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'pipWindowSizeChange', indicating the PiP window size change event.
callbackCallback<PiPWindowSize>NoCallback used to return the size of the current PiP window. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Params error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.Failed to call the API due to limited device capabilities.
1300014PiP internal error.

Example

const callback = (size: PiPWindow.PiPWindowSize) => {
  // ...
}
try {
  // Enable listening through the on API.
  pipController.on('pipWindowSizeChange', callback);
} catch (exception) {
  console.error(`Failed to enable the listener for pip window size changes. Cause code: ${exception.code}, message: ${exception.message}`);
}

try {
  // Disable the listening of a specified callback.
  pipController.off('pipWindowSizeChange', callback);
  // Unregister all the callbacks that have been registered through on().
  pipController.off('pipWindowSizeChange');
} catch (exception) {
  console.error(`Failed to disable the listener for pip window size changes. Cause code: ${exception.code}, message: ${exception.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/X4nxsm