Overview
The AdgentSDK class (alias of AdPlayer) is the main entry point for playing video ads on Smart TV platforms. It implements a “Nuclear Mute” strategy with muted, playsinline, and autoplay attributes for maximum compatibility, and includes soft-fail autoplay with interactive overlays when needed.
AdgentSDK and AdPlayer are the same class - AdgentSDK is exported as an alias for convenience.
Features
Nuclear Mute Strategy : Applies muted + playsinline + autoplay for maximum TV compatibility
Soft-fail Autoplay : Catches play() rejection and shows interactive overlay instead of crashing
Focus Management : Captures remote control keys during ad playback
VAST Parsing : Automatic wrapper resolution and tracking pixel firing
Skip Controls : Configurable skip button with countdown
Progress Tracking : Quartile events and time-based progress callbacks
Constructor
import { AdgentSDK } from 'adgent-sdk' ;
const sdk = new AdgentSDK ( config );
Configuration object for the ad player DOM element to render the ad player into
URL to the VAST XML document
Target bitrate in kbps for media file selection (prefers 1080p/720p over 4K)
Maximum depth for resolving VAST wrapper chains
Request timeout in milliseconds for VAST fetching
Time in seconds before skip button becomes available (overrides VAST skipoffset)
skipButtonText
string
default: "'Skip Ad'"
Text to display on the skip button when available
Enable debug logging to console
Custom overlay element to show when autoplay fails
Callback fired when ad playback starts
Callback fired when ad completes
Callback fired when user skips the ad
Callback fired when an error occurs
onProgress
(progress: AdProgress) => void
Callback fired on time updates during playback
Callback fired when user clicks the ad
Callback fired when user presses back button (if not provided, back will skip)
Methods
init()
Initialize the SDK: fetch VAST, create video element, and attempt autoplay.
Returns : Promise<void>
Throws : AdError if VAST parsing fails or no suitable media file is found
Process :
Fetches and parses VAST XML
Selects best media file based on targetBitrate
Creates video element with Nuclear Mute attributes
Attempts autoplay (falls back to interactive overlay on failure)
Fires impression pixels when playback starts
skip()
Skip the ad (only works if canSkip is true in state).
Fires the skip tracking event and destroys the player.
mute()
Mute the video and fire mute tracking.
unmute()
Unmute the video and fire unmute tracking.
By default, ads start muted due to the Nuclear Mute strategy. Call unmute() after playback starts if you want audio.
on()
Add an event listener for player events.
const unsubscribe = sdk . on (( event ) => {
console . log ( 'Event:' , event . type , event . data );
});
// Later: remove listener
unsubscribe ();
Parameters :
listener: AdPlayerEventListener - Function to call on each event
Returns : () => void - Unsubscribe function
Event Types :
loaded - Video metadata loaded
start - Playback started
progress - Time update (includes currentTime, duration, percentage, quartile)
quartile - Quartile reached (firstQuartile, midpoint, thirdQuartile)
pause - Playback paused
resume - Playback resumed
complete - Ad completed
skip - Ad skipped
error - Error occurred
click - Ad clicked
mute - Video muted
unmute - Video unmuted
destroy - Player destroyed
getState()
Get the current player state.
const state = sdk . getState ();
console . log ( 'Status:' , state . status );
console . log ( 'Can skip:' , state . canSkip );
Returns : AdPlayerState
interface AdPlayerState {
status : PlaybackStatus ; // Idle | Loading | Ready | Playing | Paused | Completed | Error | WaitingForInteraction
currentTime : number ;
duration : number ;
muted : boolean ;
volume : number ;
canSkip : boolean ;
skipCountdown : number ;
mediaFile : MediaFile | null ;
error : AdError | null ;
}
destroy()
Clean up all resources and remove the player from the DOM.
Removes event listeners, video element, overlays, and resets internal state.
Example Usage
Basic Setup
import { AdgentSDK } from 'adgent-sdk' ;
const container = document . getElementById ( 'ad-container' ) ! ;
const sdk = new AdgentSDK ({
container ,
vastUrl: 'https://example.com/vast.xml' ,
targetBitrate: 2500 ,
skipOffset: 5 ,
debug: true ,
onStart : () => console . log ( 'Ad started' ),
onComplete : () => {
console . log ( 'Ad completed' );
// Resume main content
},
onError : ( error ) => {
console . error ( 'Ad error:' , error );
// Handle error, resume content
}
});
await sdk . init ();
With Event Listener
const sdk = new AdgentSDK ({
container: document . getElementById ( 'ad-container' ) ! ,
vastUrl: 'https://example.com/vast.xml'
});
// Listen to all events
sdk . on (( event ) => {
switch ( event . type ) {
case 'start' :
console . log ( 'Ad playback started' );
break ;
case 'progress' :
const { currentTime , duration , percentage } = event . data ;
console . log ( `Progress: ${ percentage . toFixed ( 1 ) } %` );
break ;
case 'quartile' :
console . log ( 'Quartile:' , event . data . quartile );
break ;
case 'complete' :
console . log ( 'Ad completed' );
break ;
case 'error' :
console . error ( 'Error:' , event . data . message );
break ;
}
});
await sdk . init ();
Unmuting After Start
const sdk = new AdgentSDK ({
container: document . getElementById ( 'ad-container' ) ! ,
vastUrl: 'https://example.com/vast.xml' ,
onStart : () => {
// Unmute once playback has successfully started
sdk . unmute ();
}
});
await sdk . init ();
Manual Cleanup
const sdk = new AdgentSDK ({ /* config */ });
try {
await sdk . init ();
} catch ( error ) {
console . error ( 'Failed to initialize:' , error );
sdk . destroy ();
}
// Later, when user navigates away
window . addEventListener ( 'beforeunload' , () => {
sdk . destroy ();
});
See Also