import { useEffect, useRef, useState } from 'react'; import videojs from 'video.js'; // @ts-ignore import 'videojs-ima'; // @ts-ignore import 'videojs-contrib-ads'; import 'video.js/dist/video-js.css'; interface VASTAdPlayerProps { videoId: string; videoUrl: string; posterUrl?: string; adTagUrl?: string; onAdStart?: () => void; onAdEnd?: () => void; onAdError?: (error: any) => void; } export default function VASTAdPlayer({ videoId, videoUrl, posterUrl, adTagUrl = 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=', onAdStart, onAdEnd, onAdError }: VASTAdPlayerProps) { const videoRef = useRef(null); const playerRef = useRef(null); const [isAdPlaying, setIsAdPlaying] = useState(false); useEffect(() => { if (!videoRef.current) return; // Initialize Video.js player with IMA plugin const player = videojs(videoRef.current, { controls: true, responsive: true, fluid: true, playbackRates: [0.5, 1, 1.25, 1.5, 2], poster: posterUrl, sources: [{ src: videoUrl, type: 'video/mp4' }] }); playerRef.current = player; // Initialize ads player.ready(() => { // @ts-ignore player.ads({ debug: false, timeout: 5000 }); // Initialize IMA // @ts-ignore player.ima({ adTagUrl: adTagUrl, adsManagerLoadedCallback: () => { console.log('IMA ads manager loaded'); }, adsManagerErrorCallback: (error: any) => { console.error('IMA ads manager error:', error); onAdError?.(error); } }); // Ad event listeners player.on('ads-ad-started', () => { console.log('Ad started'); setIsAdPlaying(true); onAdStart?.(); }); player.on('ads-ad-ended', () => { console.log('Ad ended'); setIsAdPlaying(false); onAdEnd?.(); }); player.on('adserror', (event: any) => { console.error('Ad error:', event); onAdError?.(event); }); player.on('ads-request', () => { console.log('Ad request made'); }); player.on('ads-load', () => { console.log('Ad loaded'); }); }); return () => { if (playerRef.current) { playerRef.current.dispose(); playerRef.current = null; } }; }, [videoId, videoUrl, adTagUrl, posterUrl, onAdStart, onAdEnd, onAdError]); return (
{isAdPlaying && (
Oglas
)}
); }