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 takes over a shared video surface during ad breaks.
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
- 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-w3cmediapackage 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 shared surface handle.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 withautoPlay=false.
-
Register a
timeupdatelistener on the main player to detect upcoming ad breaks. -
Create the ad player initialization function. Set
autoPlaytofalseso 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, clear the main player’s surface and assign it to the ad player.Return to main content
After the ad finishes, clear the ad surface, clean up the ad player, and resume main content.Clean up resources
When your component unmounts or the player session ends, deinitialize both players and release the surface handle.Related content
- Client-side ad insertion in Vega
- Implement client-side ad insertion using the Headless approach
- W3C Media Player documentation

