### Set Start Time and Preload in Cloudflare Stream React Player Source: https://context7.com/cloudflare/stream-react/llms.txt This example shows how to control the initial playback position and preload behavior of the Cloudflare Stream player. The `startTime` prop can accept either a string in 'h:m:s' or 'm:s' format, or a number in seconds. The `preload` prop controls how much of the video should be loaded before playback starts. ```tsx import React from 'react'; import { Stream } from '@cloudflare/stream-react'; function TimestampedVideo() { return (
{/* Start at 2 minutes 30 seconds using timestamp format */} {/* Start at 150 seconds using numeric format */} {/* Start at 1 hour 12 minutes 27 seconds */}
); } ``` -------------------------------- ### Install @cloudflare/stream-react using Yarn Source: https://github.com/cloudflare/stream-react/blob/master/README.md Installs the @cloudflare/stream-react package using the Yarn package manager. This is a prerequisite for using the Cloudflare Stream React components in your project. ```sh yarn add @cloudflare/stream-react ``` -------------------------------- ### Create a Fixed Size Cloudflare Stream React Player Source: https://context7.com/cloudflare/stream-react/llms.txt This example demonstrates how to create a non-responsive, fixed-size Cloudflare Stream player by setting the `responsive` prop to `false` and explicitly defining the `height` and `width` props. This allows for precise control over the player's dimensions, useful for specific layout requirements. ```tsx import React from 'react'; import { Stream } from '@cloudflare/stream-react'; function FixedSizePlayer() { return ( ); } ``` -------------------------------- ### Handle Cloudflare Stream Player Events in React Source: https://context7.com/cloudflare/stream-react/llms.txt This example shows how to subscribe to standard HTML5 video events and Cloudflare Stream-specific advertising events. It uses React's `useState` hook to manage and display the player's playback state, current time, duration, and advertisement status. No external dependencies beyond `react` and `@cloudflare/stream-react` are required. ```tsx import React, { useState } from 'react'; import { Stream } from '@cloudflare/stream-react'; function EventDrivenPlayer() { const [playbackState, setPlaybackState] = useState('idle'); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); const [adPlaying, setAdPlaying] = useState(false); return (
{ const player = (e.target as any); setDuration(player.duration); console.log('Video duration:', player.duration); }} onPlay={() => setPlaybackState('playing')} onPause={() => setPlaybackState('paused')} onEnded={() => setPlaybackState('ended')} onTimeUpdate={(e) => { const player = (e.target as any); setCurrentTime(player.currentTime); }} onStreamAdStart={() => { setAdPlaying(true); console.log('Advertisement started'); }} onStreamAdEnd={() => { setAdPlaying(false); console.log('Advertisement ended'); }} onStreamAdTimeout={() => { console.warn('Advertisement timed out'); }} onError={(e) => { console.error('Playback error:', e); setPlaybackState('error'); }} />

State: {playbackState}

Time: {Math.floor(currentTime)}s / {Math.floor(duration)}s

{adPlaying &&

Ad Playing...

}
); } ``` -------------------------------- ### Enable Text Tracks and Captions in Cloudflare Stream React Player Source: https://context7.com/cloudflare/stream-react/llms.txt This example demonstrates how to initialize the Cloudflare Stream player with specific text tracks enabled for accessibility and multilingual support using the `defaultTextTrack` prop. Ensure that text tracks are uploaded to Cloudflare Stream first. The `defaultTextTrack` prop accepts BCP-47 language codes. ```tsx import React from 'react'; import { Stream } from '@cloudflare/stream-react'; function CaptionedVideo() { return ( { console.log('Video loaded with English captions enabled'); }} /> ); } // Note: Text tracks must be uploaded to Cloudflare Stream first // defaultTextTrack uses BCP-47 language codes (e.g., 'en', 'es', 'fr') ``` -------------------------------- ### Custom React Player with Cloudflare Stream SDK Source: https://context7.com/cloudflare/stream-react/llms.txt This snippet shows how to use the `useStreamSDK` hook to directly access and control the Cloudflare Stream player within a React component. It allows for custom event listeners, volume control, and programmatic playback. Ensure `@cloudflare/stream-react` is installed. ```tsx import React, { useEffect, useRef } from 'react'; import { useStreamSDK } from '@cloudflare/stream-react'; function CustomStreamPlayer() { const streamSdk = useStreamSDK(); const iframeRef = useRef(null); useEffect(() => { if (streamSdk && iframeRef.current) { const player = streamSdk(iframeRef.current); player.addEventListener('play', () => { console.log('Custom player started'); }); player.volume = 0.5; player.play().catch((err) => { console.error('Play failed:', err); }); return () => { // Cleanup if needed }; } }, [streamSdk]); if (!streamSdk) { return
Loading Stream SDK...
; } return (