1- import ReactDom from "react-dom"
2- import React , { useEffect , useRef , useState } from "react"
3- import Flowplayer , { useFlowplayer } from "../src"
4- import { PAUSE , PLAYING } from "@flowplayer/player/core/events"
1+ import "@flowplayer/player/flowplayer.css" ;
52
6- import "@flowplayer/player/flowplayer.css"
3+ import ReactDom from "react-dom" ;
4+ import React , { useEffect , useRef , useState } from "react" ;
5+ import flowplayer from "@flowplayer/player" ;
6+ import Flowplayer , { useFlowplayer } from "../src" ;
7+ import PreviewPlugin from "@flowplayer/player/plugins/preview" ;
78
8- const DEMO_TOKEN = "eyJraWQiOiJiRmFRNEdUam9lNVEiLCJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJjIjoie1wiYWNsXCI6NixcImlkXCI6XCJiRmFRNEdUam9lNVFcIixcImRvbWFpblwiOltcImJ1aWxkcy5mbG93cGxheWVyLmNvbVwiXX0iLCJpc3MiOiJGbG93cGxheWVyIn0.upfvSSPnB-v2ADHfbWG8ye9jDQhgwnMhZWQUqDS2DOLQbldCt9N8Atbq-gRm4GbqRRS7zoBFvvf6CgYWaV93nw"
9+ import { PAUSE , PLAYING } from "@flowplayer/player/core/events" ;
10+ import { SOURCES , DEMO_TOKEN , ANIMATED_PREVIEW } from "./config" ;
911
12+ // - Load plugins
13+ flowplayer ( PreviewPlugin ) ;
1014
11- const SOURCES = [ "//edge.flowplayer.org/bauhaus.mp4" , "//edge.flowplayer.org/functional.mp4" ]
12-
15+ // - Component
1316const Main = ( ) => {
14-
15- // Get API handle in an asynchronous manner
16- const playerRef = useRef < HTMLDivElement | null > ( null ) ;
17- const playerApi = useFlowplayer ( playerRef )
18-
19- const [ demoPlaybackState , setDemoPlaybackState ] = useState ( "paused" )
20- const [ demoSrc , setDemoSrc ] = useState ( SOURCES [ 0 ] )
21-
22- const togglePlay = ( ) => {
23- if ( ! playerApi ) return // No API available
24- playerApi . togglePlay ( )
25- }
26-
27- const toggleSrc = ( ) => {
28- const nextIndex = SOURCES . indexOf ( demoSrc ) + 1
29- setDemoSrc ( SOURCES [ nextIndex ] || SOURCES [ 0 ] )
30- }
31-
32-
33- // Listen to player events for the demo
34- useEffect ( ( ) => {
35- if ( ! playerApi ) return
36- function stateHandler ( ev : Event ) {
37- if ( ev . type === PAUSE )
38- setDemoPlaybackState ( "paused" )
39- if ( ev . type === PLAYING )
40- setDemoPlaybackState ( "playing" )
41- }
42-
43- playerApi . on ( [ PAUSE , PLAYING ] , stateHandler )
44-
45- return ( ) => { // Cleanup on unmount
46- if ( ! playerApi ) return
47- playerApi . off ( PAUSE , stateHandler )
48- playerApi . off ( PLAYING , stateHandler )
49- }
50- } , [ playerApi ] )
51-
52- //if (!Flowplayer) return null
53-
54-
55- return (
56- < div className = "container" >
57- < h1 > Flowplayer React Demo </ h1 >
58- < div className = "row" >
59- < div className = "column" >
60- < Flowplayer src = { demoSrc } token = { DEMO_TOKEN } ref = { playerRef } />
61- </ div >
62- </ div >
63- < div className = "row" >
64- < div className = "column" >
65- Playback state is: { demoPlaybackState }
66- </ div >
67- </ div >
68- < div className = "row" >
69- < div className = "column" >
70- < h2 > API handles </ h2 >
71- < button onClick = { togglePlay } > Play / pause </ button >
72- </ div >
73- < div className = "column" >
74- < h2 > Configuration changes </ h2 >
75- < button onClick = { toggleSrc } > Toggle source </ button >
76- </ div >
77- </ div >
17+ // Get API handle in an asynchronous manner
18+ const playerRef = useRef < HTMLDivElement | null > ( null ) ;
19+ const playerApi = useFlowplayer ( playerRef ) ;
20+
21+ const [ demoPlaybackState , setDemoPlaybackState ] = useState ( "paused" ) ;
22+ const [ demoSrc , setDemoSrc ] = useState ( SOURCES [ 0 ] ) ;
23+
24+ const togglePlay = ( ) => {
25+ if ( ! playerApi ) return ; // No API available
26+ playerApi . togglePlay ( ) ;
27+ } ;
28+
29+ const toggleSrc = ( ) => {
30+ const nextIndex = SOURCES . indexOf ( demoSrc ) + 1 ;
31+ setDemoSrc ( SOURCES [ nextIndex ] || SOURCES [ 0 ] ) ;
32+ } ;
33+
34+ function stateHandler ( ev : Event ) {
35+ if ( ev . type === PAUSE ) setDemoPlaybackState ( "paused" ) ;
36+ if ( ev . type === PLAYING ) setDemoPlaybackState ( "playing" ) ;
37+ }
38+
39+ // Listen to player events for the demo
40+ useEffect ( ( ) => {
41+ if ( ! playerApi ) return ;
42+ playerApi . on ( [ PAUSE , PLAYING ] , stateHandler ) ;
43+
44+ return ( ) => {
45+ // Cleanup on unmount
46+ if ( ! playerApi ) return ;
47+ playerApi . off ( PAUSE , stateHandler ) ;
48+ playerApi . off ( PLAYING , stateHandler ) ;
49+ } ;
50+ } , [ playerApi ] ) ;
51+
52+ return (
53+ < div className = "container" >
54+ < h1 > Flowplayer React Demo </ h1 >
55+ < div className = "row" >
56+ < div className = "column" >
57+ < Flowplayer
58+ src = { demoSrc }
59+ token = { DEMO_TOKEN }
60+ ref = { playerRef }
61+ opts = { {
62+ title : "Example title" ,
63+ description : "Example description" ,
64+ poster : { } ,
65+ preview : { src : ANIMATED_PREVIEW }
66+ } }
67+ / >
68+ </ div >
69+ </ div >
70+ < div className = "row" >
71+ < div className = "column" > Playback state is: { demoPlaybackState } </ div >
72+ </ div >
73+ < div className = "row" >
74+ < div className = "column" >
75+ < h2 > API handles </ h2 >
76+ < button onClick = { togglePlay } > Play / pause </ button >
77+ </ div >
78+ < div className = "column" >
79+ < h2 > Configuration changes </ h2 >
80+ < button onClick = { toggleSrc } > Toggle source </ button >
7881 </ div >
79- )
80- }
82+ </ div >
83+ </ div >
84+ ) ;
85+ } ;
8186
82- const container = document . querySelector ( "#main" )
87+ const container = document . querySelector ( "#main" ) ;
8388
84- ReactDom . render ( < Main /> , container )
89+ ReactDom . render ( < Main /> , container ) ;
0 commit comments