/
Main.tsx
80 lines (66 loc) · 2.13 KB
/
Main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { useRef, useState } from 'react';
import { Inter } from 'next/font/google';
import { Preview } from '@creatomate/preview';
import { getBasicComposition } from '@/utility/getBasicComposition';
import styles from '@/styles/Home.module.css';
const inter = Inter({ subsets: ['latin'] });
export const Main: React.FC = () => {
const [isLoading, setIsLoading] = useState(true);
// Reference to the preview
const previewRef = useRef<Preview>();
const setUpPreview = (htmlElement: HTMLDivElement) => {
// Clean up an older instance of the preview SDK
if (previewRef.current) {
previewRef.current.dispose();
previewRef.current = undefined;
}
// Initialize a preview. Make sure you provide your own public token, which can be found in your dashboard under Project Settings
const preview = new Preview(htmlElement, 'player', 'public-0x6hcqpfhrhw16d67ogth7ry');
preview.onReady = async () => {
// Once the SDK is ready, create a basic video scene
await preview.setSource(getBasicComposition());
// Skip to 2 seconds into the video
await preview.setTime(2);
setIsLoading(false);
};
previewRef.current = preview;
};
const playVideo = async () => {
await previewRef.current?.play();
};
const pauseVideo = async () => {
await previewRef.current?.pause();
};
return (
<main className={`${styles.main} ${inter.className}`}>
<div className={styles.center}>
{isLoading && 'Loading...'}
<div
className={styles.container}
ref={(htmlElement) => {
if (htmlElement && htmlElement !== previewRef.current?.element) {
setUpPreview(htmlElement);
}
}}
/>
<div className={styles.controls}>
<button
onClick={async () => {
await playVideo();
}}
>
Play video
</button>
<button
onClick={async () => {
await pauseVideo();
}}
>
Pause video
</button>
</div>
</div>
</main>
);
};