How Do I Integrate It?

Dacast Player JS API integration documentation

Dacast JavaScript API

Overview

The Dacast Player exposes a global window.dacast object. Use the dacast() function to initialize a player, then control it via the player instance or postMessage (for iframe embeds).

JS file: https://prod-player.dacast.com/js/player.js


1. Initialization

const player = dacast(contentId, containerId [, options]);
ParameterTypeRequiredDescription
contentIdstringYesThe Dacast content ID
containerIdstringYesID of the HTML element to mount into
optionsPlayerOptionsNoOptional configuration (see below)

PlayerOptions

FieldTypeDescription
widthstringCSS width of the player (e.g. "640px")
heightstringCSS height of the player (e.g. "360px")
signedKeystringSigned key for protected content
startTimenumberOffset start time in seconds
GACodestringGoogle Analytics code — format: G-XXXX

Example:

<div id="my-player"></div>
<script>
  const player = dacast("abc123", "my-player", {
    width: "640px",
    height: "360px"
  });
</script>

2. Player Instance API

After initialization, the player is also accessible globally:

const player = window.dacast.players["your-content-id"];

Note: The player object is available immediately, but methods are no-ops until the player is ready. Use onReady() to safely call methods after initialization.

Methods

MethodSignatureDescription
play() => voidStarts playback
pause() => voidPauses playback
seekTo(time: number) => voidSeeks to a position in seconds
mute() => voidMutes the player
muteOn() => voidMutes the player
muteOff() => voidUnmutes the player
muteToggle() => voidToggles mute based on current state
setVolume(time: number) => voidSet volume — 0 (min) to 1 (max)
getVolume() => numberGet current volume — 0 (min) to 1 (max)
currentTime() => numberReturns current playback position in seconds
getTime(callback: (time: number) => void) => voidCalls callback with current time
onReady(callback: () => void) => voidFires callback when player is ready
loadVideo(contentId: string) => voidLoads new content by ID
getContentInfo() => ContentInfoReturns metadata about current content
getActiveMedia() => ContentInfoAlias for getContentInfo
getElement() => HTMLElementReturns the player's DOM container
dispose() => voidUnloads the player
fullscreen(boolean) => voidSet/unset fullscreen mode
activeViewers() => numberCurrent viewer count (live only, if count enabled in theme)

ContentInfo Object

{
  contentId: string
  title: string
  description: string
  splashscreenUrl: string
  thumbnailUrl: string
}

Example:

const player = dacast("abc123", "my-player");

player.onReady(() => {
  player.seekTo(30);

  const info = player.getContentInfo();
  console.log(info.title, info.description);

  player.dispose();
});

3. Player Events

Subscribe with player.on('event_name', callback) or player.onEventName(callback) — both return a Subscriber.

Unsubscribe with player.off('event_name', callback) or subscriber.unsubscribe().

interface Subscriber {
  unsubscribe: () => void
}

Event Methods

MethodSignatureDescriptionEvent name
onReady(callback: () => void) => SubscriberListen for Ready eventready
onPlay(callback: () => void) => SubscriberListen for Play eventplay
onPause(callback: () => void) => SubscriberListen for Pause eventpause
onComplete(callback: () => void) => SubscriberFires at end of current mediacomplete
onFullScreenEnter(callback: () => void) => SubscriberFires when player enters fullscreenfullscreen_enter
onFullScreenExit(callback: () => void) => SubscriberFires when player exits fullscreenfullscreen_exit
onActiveViewersChange(callback: (count: number) => void) => SubscriberLive only — fires when active viewer count changesactiveviewers_change
onError(callback: (error) => void) => SubscriberListen for playback errorserror
onPlayerAdError(callback: (error) => void) => SubscriberListen for ad errorsad_error
on(eventName: string) => SubscriberListen to any event by nameN/A
off(eventName: string, callback: func) => voidDetach a callback from an eventN/A