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 JS approach. This approach uses direct React Native component integration where VideoPlayer instances are created and managed within the UI component layer. You’ll set up a dual-player architecture with a main player for content and an ad player 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 references and imports
  • Set up the video surfaces for shared or dual surface mode
  • Detect an ad marker and pre-initialize the ad player
  • 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/react-native-w3cmedia package installed
  • Main content player already initialized and playing
  • Basic knowledge of React Native and TypeScript

Set up player references

Import the required components and create refs for the main player, ad player, 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 cache its handle in cachedSurface. Both players use this surface in turn.
  • Dual surface: Render a second KeplerVideoSurfaceView on top of the main surface for the ad player, and cache its handle in adCachedSurface.
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

When the app detects an upcoming ad break (for example, 5 seconds before the ad start time), pre-initialize the ad player and begin buffering the ad content with autoPlay=false.
  1. Register a timeupdate listener on the main player to detect upcoming ad breaks.
  2. Create the ad player initialization function. Set autoPlay to false so you control when playback starts.

Wait for the ad to buffer, then pause main content

After the ad player signals it is buffered (canplay) and the ad start time is reached, pause the main content.

Switch the surface and play the ad

When the main content pauses, give the ad player a surface and play the ad.
  • Shared surface: Clear the main player’s surface, then assign the same surface to the ad player.
  • Dual surface: Assign the ad surface to the ad player. Don’t clear the main player’s surface.

Return to main content

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

Clean up resources

When your component unmounts or the player session ends, deinitialize both players and release the surface handle.

最終更新日 2026年10月5日