-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathplayer.tsx
115 lines (103 loc) · 3.1 KB
/
player.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import './player.css';
import { useEffect, useRef, useState } from 'react';
import {
isHLSProvider,
MediaPlayer,
MediaProvider,
Poster,
Track,
type MediaCanPlayDetail,
type MediaCanPlayEvent,
type MediaPlayerInstance,
type MediaProviderAdapter,
type MediaProviderChangeEvent,
} from '@vidstack/react';
import {
DefaultAudioLayout,
defaultLayoutIcons,
DefaultVideoLayout,
} from '@vidstack/react/player/layouts/default';
import { textTracks } from './tracks';
export function Player() {
let player = useRef<MediaPlayerInstance>(null),
[src, setSrc] = useState('');
useEffect(() => {
// Initialize src.
changeSource('audio');
// Subscribe to state updates.
return player.current!.subscribe(({ paused, viewType }) => {
// console.log('is paused?', '->', paused);
// console.log('is audio view?', '->', viewType === 'audio');
});
}, []);
function onProviderChange(
provider: MediaProviderAdapter | null,
nativeEvent: MediaProviderChangeEvent,
) {
// We can configure provider's here.
if (isHLSProvider(provider)) {
provider.config = {};
}
}
// We can listen for the `can-play` event to be notified when the player is ready.
function onCanPlay(detail: MediaCanPlayDetail, nativeEvent: MediaCanPlayEvent) {
// ...
}
function changeSource(type: string) {
switch (type) {
case 'audio':
setSrc('https://files.vidstack.io/sprite-fight/audio.mp3');
break;
case 'video':
setSrc('https://files.vidstack.io/sprite-fight/720p.mp4');
break;
case 'hls':
setSrc('https://files.vidstack.io/sprite-fight/hls/stream.m3u8');
break;
case 'youtube':
setSrc('youtube/_cMxraX_5RE');
break;
case 'vimeo':
setSrc('vimeo/640499893');
break;
}
}
return (
<>
<MediaPlayer
className="player"
title="Sprite Fight"
src={src}
crossOrigin
playsInline
onProviderChange={onProviderChange}
onCanPlay={onCanPlay}
ref={player}
>
<MediaProvider>
<Poster
className="vds-poster"
src="https://files.vidstack.io/sprite-fight/poster.webp"
alt="Girl walks into campfire with gnomes surrounding her friend ready for their next meal!"
/>
{textTracks.map((track) => (
<Track {...track} key={track.src} />
))}
</MediaProvider>
{/* Layouts */}
<DefaultAudioLayout icons={defaultLayoutIcons} />
<DefaultVideoLayout
icons={defaultLayoutIcons}
thumbnails="https://files.vidstack.io/sprite-fight/thumbnails.vtt"
/>
</MediaPlayer>
<div className="src-buttons">
<button onClick={() => changeSource('audio')}>Audio</button>
<button onClick={() => changeSource('video')}>Video</button>
<button onClick={() => changeSource('hls')}>HLS</button>
<button onClick={() => changeSource('youtube')}>YouTube</button>
<button onClick={() => changeSource('vimeo')}>Vimeo</button>
</div>
</>
);
}