Preview Player Demo
Here’s how the embedded preview player looks when integrated into your application:Pictory Preview Player embedded in an iframe with full playback controls
Stock Visual Watermarks: The preview may display watermarked stock visuals from media libraries. These watermarks are automatically removed when you render the final video.
What You’ll Learn
Embed Preview
Display the preview player in an iframe
Player Communication
Use postMessage API to communicate with the player
Real-time Updates
Update preview elements without re-rendering
Event Handling
Handle player events like loaded and error states
Before You Begin
Make sure you have:- A Pictory API key (get one here)
- A completed storyboard preview job with a
previewUrl - Basic knowledge of JavaScript and iframe communication
Workflow Overview
Step 1: Create Storyboard Preview
First, create a storyboard preview using the Create Storyboard Preview API:Step 2: Get Preview URL and Render Params
Poll the job status until it’s completed to get thepreviewUrl and renderParams:
Background Element Structure
ThebackgroundElement is the core visual element for each scene. Key properties you can modify:
| Property | Type | Description |
|---|---|---|
url | string | The media URL for the background video or image |
visualUrl | string | Same as url, the visual media source |
type | string | Media type: video or image |
visualType | string | Same as type, the visual media type |
duration | number | Duration in seconds |
loop | boolean | Whether to loop the video |
mute | boolean | Whether to mute video audio |
backgroundColor | string | Fallback background color (RGBA format) |
objectMode | string | How media fits: cover, contain, fill |
Step 3: Embed Preview Player in iframe
Basic HTML Implementation
Preview Editor Class
Create apreview-editor.js file with the PreviewEditor class that handles iframe communication:
Step 4: React Integration
Here’s a complete React component for embedding the preview player:Usage in Your App
Step 5: Update Preview Elements in Real-time
The key feature is updating preview elements without re-rendering the entire video. When you modify theelements array and call updatePreview(), the player updates instantly.
Replacing Background Visual
To replace a scene’s background video or image, find thebackgroundElement and update its url and visualUrl properties:
Background Element Properties
| Property | Type | Description | Example |
|---|---|---|---|
url | string | Primary media URL | "https://cdn.com/video.mp4" |
visualUrl | string | Visual media URL (same as url) | "https://cdn.com/video.mp4" |
type | string | Media type | "video" or "image" |
visualType | string | Visual media type | "video" or "image" |
duration | number | Duration in seconds | 6.86 |
loop | boolean | Loop video playback | true |
mute | boolean | Mute video audio | true |
backgroundColor | string | Fallback color | "rgba(255,255,255,1)" |
objectMode | string | Media fit mode | "cover", "contain", "fill" |
Example: Building a Background Visual Editor
Replacing Multiple Backgrounds
PostMessage API Reference
Messages Sent TO the Preview Player
| Message | Payload | Description |
|---|---|---|
UPDATE_PREVIEW_ELEMENTS | { elements: [] } | Update the preview with new element data |
Messages Received FROM the Preview Player
| Message | Payload | Description |
|---|---|---|
ON_LOADED | None | Player has finished loading and is ready |
ON_ERROR | { error: string } | An error occurred in the player |
Message Format
All messages follow this structure:Complete Integration Example
Here’s a complete end-to-end example:Best Practices
Handle Loading States
Handle Loading States
Always show a loading indicator while the preview is initializing. The player may take a few seconds to load depending on network conditions.
Clean Up Resources
Clean Up Resources
Always call
close() when unmounting the component or navigating away to prevent memory leaks and remove event listeners.Debounce Updates
Debounce Updates
When building live editors, debounce updates to prevent overwhelming the player with rapid changes.
Error Handling
Error Handling
Implement comprehensive error handling for network failures and player errors.
Troubleshooting
Preview not loading
Preview not loading
Problem: The iframe shows a blank screen or loading forever.Solutions:
- Verify the
previewUrlis valid and not expired - Check browser console for CORS or security errors
- Verify your domain is allowed to embed the preview
Updates not reflecting
Updates not reflecting
Problem: Calling
updatePreview() doesn’t change the preview.Solutions:- Ensure
editor.loadedistruebefore calling update - Check that elements array structure is correct
- Verify the message is being received (check browser console)
- Wait for the
ON_LOADEDevent before updating
postMessage not working
postMessage not working
Problem: Messages are not being received by the iframe.Solutions:
- Verify
iframe.contentWindowis not null - Check that you’re listening for messages correctly
- Ensure the message origin matches expected sources
- Use browser dev tools to inspect postMessage traffic
Memory leaks
Memory leaks
Problem: Application becomes slow after multiple preview loads.Solutions:
- Always call
editor.close()when done - Remove event listeners in cleanup functions
- Clear the iframe src before removing
- Use React’s useEffect cleanup or componentWillUnmount
