Amazon Developer

as

Settings
Sign out
Notifications
Alexa
Amazonアプリストア
Ring
AWS
ドキュメント
Support
Contact Us
My Cases
開発
設計と開発
公開
リファレンス
サポート
メインコンテンツへスキップ
In this tutorial, you’ll implement client-side ad insertion (CSAI) in your Vega app using the Headless approach. This approach uses a client-server architecture where IPlayerClient instances in the UI layer communicate with VideoPlayer instances in a separate service layer via JavaScript Interface (JSI)-based inter-process communication (IPC). You’ll set up a dual-player architecture with a main player client for content and an ad player client that plays ads during ad breaks, either on a video surface shared with the main player or on its own dedicated surface. The examples in this tutorial demonstrate pre-buffered ads for seamless ad transitions. For background on CSAI concepts, techniques, and sync strategies, see Client-side ad insertion in Vega. In this tutorial, you learn how to:
  • Set up player client references and imports
  • Set up the video surfaces for shared or dual surface mode
  • Detect an ad marker and pre-initialize the ad player client
  • Wait for the ad to buffer, then pause main content
  • Switch the video surface and play the ad
  • Return to main content after the ad ends

Prerequisites

  • Vega development environment set up
  • Vega SDK v0.22 or later
  • @amazon-devices/kepler-player-client and @amazon-devices/kepler-player-server packages installed
  • Main content player client already initialized and connected to the service
  • Basic knowledge of React Native and TypeScript

Set up player client references

Import the required packages and create refs for the main player client, ad player client, session IDs, and surface handles.

Set up the video surfaces

Choose a surface mode. For the differences between the modes, see Surface modes.
  • Shared surface: Render one KeplerVideoSurfaceView and store its handle in surfaceHandleRef. Both player sessions use this surface in turn.
  • Dual surface: Render a second KeplerVideoSurfaceView on top of the main surface for the ad player session, and store its handle in adSurfaceHandleRef.
If your ads can have a different aspect ratio than the main content, see Handling ads with different aspect ratios.

Detect the ad marker and pre-initialize the ad player client

When the position listener detects an upcoming ad break, pre-initialize the ad player client with autoPlay=false and register listeners to know when the ad is buffered and when it ends.
  1. Set up a position listener to detect upcoming ad breaks.
  2. Create the ad player client initialization function. Register a status listener to detect the READY and ENDED states. On platforms where IPlayerSessionState.READY is not available, the service sends a playerReady message as a fallback.

Wait for the ad to buffer, then pause main content

After the ad player signals READY (buffered) and the position listener detects the ad start time is reached, pause the main content.

Switch the surface and play the ad

When the main content status changes to PAUSED, give the ad player session a surface and play the ad.
  • Shared surface: Clear the main session’s surface, then set the same surface on the ad session.
  • Dual surface: Set the ad surface on the ad session. Don’t clear the main session’s surface.

Return to main content

After the ad ends, clear the ad surface, clean up the ad player client, and resume main content. In shared surface mode, set the surface on the main session again before you resume. In dual surface mode, the main session still has its surface.

Clean up resources

When your component unmounts or the player session ends, unload the ad player client and release resources.

最終更新日 2026年10月5日