|
1 | 1 | import Head from 'next/head'; |
2 | | -import { useRef, useState, useEffect } from "react"; |
| 2 | +import Script from "next/script"; |
| 3 | +import { useRef, useState } from "react"; |
3 | 4 | import MuxVideoAds from "@mux/mux-video/ads/react"; |
4 | | -import '../post-video.css' |
5 | | - |
6 | | -const INITIAL_AUTOPLAY = false; |
7 | | -const INITIAL_MUTED = false; |
8 | 5 |
|
9 | 6 | function MuxVideoPage() { |
10 | 7 | const mediaElRef = useRef(null); |
11 | | - const [autoplay, setAutoplay] = useState<"muted" | boolean>(INITIAL_AUTOPLAY); |
12 | | - const [muted, setMuted] = useState(INITIAL_MUTED); |
13 | | - const [paused, setPaused] = useState<boolean | undefined>(true); |
14 | | - const [sdkLoaded, setSdkLoaded] = useState(false); |
15 | | - |
16 | | - const mainVideo = { |
17 | | - imageUrl: "https://d.newsweek.com/en/full/2616049/picture-video.jpg?w=480&h=270&q=75&f=234a33201b1c5adb080a1bec0cb4a4a0", |
18 | | - title: "US Official Cars Seen Leaving Greenland Capital After Vance Scales Back Visit", |
19 | | - idx: 2 |
20 | | - }; |
21 | | - |
22 | | - const relatedVideos = [ |
23 | | - { |
24 | | - imageUrl: "https://d.newsweek.com/en/full/2614934/picture-video.jpg?w=480&h=270&q=75&f=8dbdcf86a118c0f5afb2cba69bd4af24", |
25 | | - title: "JD Vance Announces He Will Join His Wife On Visit To Greenland", |
26 | | - idx: 0 |
27 | | - }, |
28 | | - { |
29 | | - imageUrl: "https://d.newsweek.com/en/full/2616059/reason-why-woman-trains-specific-arm.jpg?w=480&h=270&q=75&f=5000e727dbcc4e9e9d1ca21b1215c993", |
30 | | - title: "Reason Why Woman Trains Specific Arm", |
31 | | - idx: 1 |
32 | | - }, |
33 | | - { |
34 | | - imageUrl: "https://d.newsweek.com/en/full/2616049/picture-video.jpg?w=480&h=270&q=75&f=234a33201b1c5adb080a1bec0cb4a4a0", |
35 | | - title: "US Official Cars Seen Leaving Greenland Capital After Vance Scales Back Visit", |
36 | | - idx: 2 |
37 | | - } |
38 | | - ]; |
39 | | - |
40 | | - useEffect(() => { |
41 | | - // Dynamically load the IMA SDK |
42 | | - const loadImaSdk = () => { |
43 | | - const script = document.createElement("script"); |
44 | | - script.src = "https://imasdk.googleapis.com/js/sdkloader/ima3.js"; |
45 | | - script.async = true; |
46 | | - script.onload = () => { |
47 | | - setSdkLoaded(true); // Mark SDK as loaded |
48 | | - console.log("Google IMA SDK loaded"); |
49 | | - }; |
50 | | - script.onerror = () => { |
51 | | - setSdkLoaded(true); |
52 | | - }; |
53 | | - document.head.appendChild(script); |
54 | | - }; |
55 | | - |
56 | | - if (!window.google || !window.google.ima) { |
57 | | - loadImaSdk(); |
58 | | - } else { |
59 | | - setSdkLoaded(true); |
60 | | - } |
61 | | - |
62 | | - return () => { |
63 | | - // Cleanup by removing the script |
64 | | - const scriptElement = document.querySelector('script[src="https://imasdk.googleapis.com/js/sdkloader/ima3.js"]'); |
65 | | - if (scriptElement) { |
66 | | - scriptElement.remove(); |
67 | | - } |
68 | | - }; |
69 | | - }, []); |
| 8 | + const [sdkLoaded, setSdkLoaded] = useState(false); |
70 | 9 |
|
71 | 10 | return ( |
72 | 11 | <> |
73 | 12 | <Head> |
74 | 13 | <title><MuxVideoAds/> Demo</title> |
75 | 14 | </Head> |
76 | 15 |
|
| 16 | + <Script |
| 17 | + src="https://imasdk.googleapis.com/js/sdkloader/ima3.js" |
| 18 | + onLoad={() => { |
| 19 | + setSdkLoaded(true); |
| 20 | + console.log("Google IMA SDK loaded"); |
| 21 | + }} |
| 22 | + strategy="afterInteractive" |
| 23 | + /> |
| 24 | + |
77 | 25 | {sdkLoaded && <MuxVideoAds |
78 | 26 | ref={mediaElRef} |
79 | 27 | playbackId="23s11nz72DsoN657h4314PjKKjsF2JG33eBQQt6B95I" |
80 | 28 | controls |
81 | | - // allow playback with ad blocker |
82 | | - allowAdBlocker={true} |
83 | | - autoplay={autoplay} |
84 | | - muted={muted} |
85 | 29 | playsInline={true} |
86 | | - maxResolution="2160p" |
87 | | - minResolution="540p" |
88 | | - renditionOrder="desc" |
89 | 30 | preferPlayback="mse" |
| 31 | + allowAdBlocker={true} |
90 | 32 | adTagUrl="https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=" |
91 | | - onPlay={() => { |
92 | | - setPaused(false); |
93 | | - }} |
94 | | - onPause={() => { |
95 | | - setPaused(true); |
96 | | - }} |
97 | | - onEnded={() => { |
98 | | - |
99 | | - }} |
100 | | - > |
101 | | - </MuxVideoAds>} |
| 33 | + />} |
102 | 34 | </> |
103 | 35 | ); |
104 | 36 | } |
|
0 commit comments