Home/docs/guide/OpenHarmony / OHPM

OpenHarmony / OHPM integration

Native ArkTS SDK

erika is the native ArkTS SDK for OpenHarmony. Your app obtains a native surface through XComponent and uses ErikaPlayer to drive playback control, event handling, and per-frame render scheduling.

Installation and import#

Install the public release 0.3.8 from the root directory of your DevEco Studio project:

shell

ohpm install erika@0.3.8

For package details, see the OHPM details page. The source module is in the packages/erika_ohos directory of the official repository.

Surface lifecycle and controller#

ErikaPlayer wraps the underlying Presenter instance. In the XComponentController callbacks, the host binds the native surface and keeps its size in sync:

ErikaSurfaceController.ets

import { ErikaNativeResponse, ErikaPlayer } from 'erika';

export class ErikaSurfaceController extends XComponentController {
  private readonly player: ErikaPlayer = new ErikaPlayer();
  private attached: boolean = false;
  private pendingUri: string = '';

  onSurfaceCreated(surfaceId: string): void {
    /* Record surface creation */
  }

  onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void {
    if (!this.attached) {
      this.player.attachSurface({
        surfaceId: BigInt(surfaceId),
        width: rect.surfaceWidth,
        height: rect.surfaceHeight,
        scale: 1.0,
      });
      this.attached = true;
    } else {
      this.player.resizeSurface(rect.surfaceWidth, rect.surfaceHeight, 1.0);
    }
    this.startPendingPlay();
  }

  onSurfaceDestroyed(_surfaceId: string): void {
    if (this.attached) {
      this.player.detachSurface();
      this.attached = false;
    }
    this.player.dispose();
  }

  openAndPlay(uri: string): void {
    this.pendingUri = uri;
    this.startPendingPlay();
  }

  renderFrame(timeSeconds: number): ErikaNativeResponse {
    return this.player.renderTick(timeSeconds);
  }

  private startPendingPlay(): void {
    if (!this.attached || this.pendingUri.length === 0) {
      return;
    }
    const uri: string = this.pendingUri;
    this.pendingUri = '';
    this.player.open(uri);
    this.player.play();
  }
}

Using it in an ArkUI page#

Index.ets

import { ErikaSurfaceController } from './ErikaSurfaceController';

@Entry
@Component
struct VideoPlayerPage {
  private readonly controller: ErikaSurfaceController = new ErikaSurfaceController();
  private timerId: number = -1;

  aboutToAppear(): void {
    /* Simplified example: call renderTick from a timer. Your app can use a display frame callback instead */
    let startTime = Date.now();
    this.timerId = setInterval(() => {
      const nowSeconds = (Date.now() - startTime) / 1000.0;
      this.controller.renderFrame(nowSeconds);
    }, 16);
  }

  aboutToDisappear(): void {
    if (this.timerId !== -1) {
      clearInterval(this.timerId);
      this.timerId = -1;
    }
  }

  build() {
    Column() {
      XComponent({
        id: 'erika_video_surface',
        type: XComponentType.SURFACE,
        controller: this.controller,
      })
        .width('100%')
        .aspectRatio(1.777)
        .onLoad(() => {
          this.controller.openAndPlay('https://example.com/stream.mp4');
        })
    }
    .width('100%')
    .height('100%')
  }
}

ArkTS ErikaPlayer public API#

CategoryMethodsDescription
Playback controlopen(uri) / play() / pause() / stop() / close() / seek(positionMicros) / setPlaybackRate(rate) / setVolume(volume)Opens a media source, plays, pauses, seeks (in microseconds), and sets the playback rate and volume.
Surface managementattachSurface(options) / resizeSurface(w, h, scale) / detachSurface()Binds the XComponent native surface, syncs the pixel size, and releases the surface.
Render schedulingrenderTick(timeSeconds) / audioOnlyTick() / pollEvent()Renders frames on the display clock, schedules audio-only playback (no video), and polls state events.
Additional featurescaptureFrame(w, h) / registerSubtitleMemoryFont(bytes) / dispose()Captures the current frame's pixels, registers an in-memory subtitle font, and destroys the player instance.

The core 0.3.1 OpenHarmony native build fixes text subtitles rendered as boxes. As of 2026-11-26 the public OHPM package is 0.3.8; check the native runtime and package versions separately.

HTTP cache options (since 0.2.1)#

As of 2026-10-30, OHPM 0.3.0 is publicly available and includes the following HTTP back-buffer parameters.

httpReadAheadBytes sets how many bytes to read ahead (default 2 MiB). httpBackBufferBytes sets how much already-played data to keep (default 16 MiB). Omit either field or pass 0 to use its default. You can also set the read-ahead default with ERIKA_HTTP_READAHEAD_BYTES.

Backward seeks within the back buffer reuse data that is already downloaded. High-bitrate video needs a larger budget: keeping about 10 seconds of an 80 Mbit/s video takes about 100 MB. The cache is limited by bytes, so it does not correspond to a fixed number of playback seconds.

ArkTS

player.open('https://example.com/video.mp4', {
  httpReadAheadBytes: 8 * 1024 * 1024,
  httpBackBufferBytes: 128 * 1024 * 1024,
});
player.play();

Version 0.3.4 adds asynchronous N-API entry points; the legacy ErikaOhosPlayerFromId returns null. Hosts should use bridge methods instead of accessing native player objects across threads.

liberika v0.3.8 Section Guide License MPL-2.0