├── .gitignore
├── LICENSE
├── README.md
├── index.html
├── package-lock.json
├── package.json
├── public
└── vite.svg
├── src
├── App.css
├── App.tsx
├── index.css
├── lib
│ ├── TVPlayer.test.tsx
│ ├── TVPlayer.tsx
│ ├── TVPlayerActivity.ts
│ ├── TVPlayerStore.ts
│ ├── TVPlayerTypes.ts
│ ├── TVPlayerUI
│ │ ├── TVPlayerUI.test.tsx
│ │ ├── TVPlayerUI.tsx
│ │ ├── index.ts
│ │ └── tv-player-ui.scss
│ ├── index.ts
│ └── tv-player.scss
├── main.tsx
└── vite-env.d.ts
├── tests
└── setup.js
├── tsconfig.json
├── tsconfig.node.json
├── vite.config.demo.ts
├── vite.config.lib.ts
└── vite.config.ts
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 |
6 | # production
7 | /dist
8 | /demo
9 |
10 | # misc
11 | .DS_Store
12 | .env.local
13 | .env.development.local
14 | .env.test.local
15 | .env.production.local
16 |
17 | npm-debug.log*
18 | yarn-debug.log*
19 | yarn-error.log*
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Lewis Hunt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
19 | A React media player component for TV devices. With custom UI and arrow-key navigation, it can play a variety of URLs like file paths, HLS/DASH streams and even YouTube links directly. Packaged as an npm library.
Click on the image to try out the demo on a desktop browser
24 |
25 | ## Installation
26 |
27 | ```bash
28 | npm install react-tv-player
29 | ```
30 |
31 | ## Usage
32 |
33 | Just import the player and render it in your app or page with [props](#props):
34 |
35 | ```jsx
36 | import { TVPlayer } from "react-tv-player";
37 |
38 | function App() {
39 | return (
40 | <>
41 |
42 | >
43 | );
44 | }
45 | ```
46 |
47 | ### [👉 Try out the Demo](https://lewhunt.github.io/react-tv-player/) (on TV and desktop browsers)
48 |
49 | The demo source code App.tsx illustrates how the component can be initialised with more [props](#props) such as metadata, [custom buttons](#custom-buttons), preview images and multiple media, enabling the user to cycle through videos with next/previous buttons:
50 |
51 | ```jsx
52 |
61 | ```
62 |
63 |
Here is a short video of the demo runnning on a browser:
64 |
65 | https://github.com/lewhunt/react-tv-player/assets/9886284/7baa4b75-491b-49f3-8cf1-698ae7f55941
66 |
67 | ## Why Another Player?
68 |
69 | I've dedicated years working with various OTT players on big-screen devices. During this journey, two persistent challenges surfaced time and again: performant UI and compatible media encodings. These hurdles often forced us to heavily customise libraries and tackle streaming difficulties, leading to added costs and frustrating delays. 😫
70 |
71 | Enter ReactTVPlayer, an open-source media player component for TV devices that lowers the barrier to entry and seamlessly integrates with your React applications. :muscle:
72 |
73 | ## What Sets It Apart?
74 |
75 | It's designed for TV experiences out of the box, complete with customisable UI buttons and intuitive cursor plus arrow key navigation. 🎮 In addition to handling HLS and Dash streams effortlessly, it tackles the formidable challenge of playing YouTube and Vimeo urls directly on TV, saving you the hassle and cost of additional video encoding. 🎉
76 |
77 | ## How Does It Work?
78 |
79 | Under the hood, this component harnesses the power of open-source libraries like Norigin Media's spatial navigation hook. It builds upon the excellence of React Player, which utilises hls.js and dash.js. Powered by React TypeScript (although you don't need to use TypeScript to make the most of it), this library is packaged efficiently using Vite, making integration a breeze. 🙌
80 |
81 | ## Key Features
82 |
83 | - Versatility: Customisable UI buttons, title metadata and preview images to suit your needs. It can effortlessly handle a variety of URLs, from mp4 file paths and HLS/DASH streams to services like YouTube and Vimeo.
84 | - Intuitive Navigation: The player has been designed with TV experiences in mind. Cursor and arrow-key navigation make the user experience smooth and intuitive across big-screen platforms.
85 | - YouTube Integration: One of its unique strengths is its ability to play URLs directly from the likes of YouTube, Vimeo, SoundCloud and Twitch, eliminating the need for additional video encoding when it's not needed.
86 | - DRM Considerations: While it supports HLS AES Encryption, it’s built with flexibility in mind, allowing for future DRM integration with hls.js and dash.js.
87 | - Broad Device Support: From Amazon Fire TV, LG webOS and Samsung Tizen Smart TVs to Xbox consoles and desktop web browsers, ReactTVPlayer covers most big-screen devices running web apps with post-2017 Chromium browsers.
88 |
89 |
90 |
91 | ## Props
92 |
93 | The full list of props are below. Media related values such as `playing`, `loop` and `muted` are also mapped to state which can be accessed via the [useTVPlayerStore hook](#usetvplayerstore-hook) instead of updating props.
94 |
95 | | Prop | Description | Default |
96 | | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------ |
97 | | `url` | The url of the media to play ◦ This can be an embedded url from YouTube/SoundCloud, a file path or a HLS or Dash manifest stream |
98 | | `playing` | Set to `true` or `false` to pause or play the media. ◦ Set to `true` to autoplay the media (muted may also be needed in some browsers) | `false` |
99 | | `loop` | Set to `true` to loop the media | `false` |
100 | | `controls` | Set to `true` to display native HTML5 media controls instead of custom TV Player UI controls | `false` |
101 | | `light` | Set to `true` or a url `string` to show a preview image, which then loads the full player on selecting play ◦ Pass in true to use the default preview image associated with an embeded media url (e.g. YouTube/SoundCloud urls) ◦ Pass in an image URL to override any default preview image | `false` |
102 | | `volume` | Set the volume of the player, between `0` and `1` | `null` |
103 | | `muted` | Set to `true` to mute the player ◦ may be required if you intend to autoplay media | `false ` |
104 | | `playbackRate` | Set the playback rate of the player ◦ Only supported by YouTube, Wistia, and file paths | `1` |
105 | | `disableFullscreen` | Set to true to disable the defaulted fullscreen width and height of the player. ◦ Width and height values below should be defined ◦ See App.tsx useEffect for example of toggling body properties based on fullscreen state | `false` |
106 | | `disableInitNav` | Set to true to disable initialisation of the norigin spatial navigation library. ◦ Set this if you are already initialising the navigation library in your own app | `false` |
107 | | `disableNav` | Set to true to pause arrow key navigation of the norigin spatial navigation library in the player. ◦ Set this if you show a custom menu and want to disable the player's key event handlers. | `false` |
108 | | `width` | Set the width of the player when fullscreen is disabled | `100%` |
109 | | `height` | Set the height of the player when fullscreen is disabled | `350px` |
110 | | `style` | Add [inline styles](https://facebook.github.io/react/tips/inline-styles.html) to the root element | `{}` |
111 | | `customButtons` | Specify a collection of [custom buttons](#custom-buttons) for the player UI ◦ A set of default buttons will be used otherwise. | `null` |
112 | | `title` | Set a `string` title for the current media. ◦ Embedded media urls such as YouTube will attempt to pull in the default media title if not overridden here. | |
113 | | `subTitle` | Set a `string` sub-title for the current media. ◦ Embedded media urls such as YouTube will attempt to pull in the default author name if not overridden here. | |
114 | | `mediaCount` | Set the total `number` of media items if you have multiple media and want player to display next and previous buttons | `0` |
115 | | `mediaIndex` | Set the initial media index `number` if you have multiple media and want player to handle next and previous buttons | `0` |
116 | | `hideControlsOnArrowUp` | Set to `true` to hide the player UI by using the up arrow key when focussed on the player buttons | `false` |
117 |
118 |
119 |
120 | ## Callback Props
121 |
122 | Callback props take a function that gets fired on various player events and UI button actions:
123 |
124 | | Prop | Description |
125 | | -------------------- | --------------------------------------------------------------------------------------------------------- |
126 | | `onReady` | Called when media is loaded and ready to play. If `playing` is set to `true`, media will play immediately |
127 | | `onStart` | Called when media starts playing |
128 | | `onPlay` | Called when media starts or resumes playing after pausing or buffering |
129 | | `onPause` | Called when media is paused |
130 | | `onBuffer` | Called when media starts buffering |
131 | | `onEnded` | Called when media finishes playing ◦ Does not fire when `loop` is set to `true` |
132 | | `onError` | Called when an error occurs whilst attempting to play media |
133 | | `onSkipBackPress` | Called when the Skip Back button is pressed |
134 | | `onSkipForwardPress` | Called when the Skip Forward button is pressed |
135 | | `onPreviousPress` | Called when the Previous button is pressed |
136 | | `onNextPress` | Called when the Next button is pressed |
137 | | `onLikePress` | Called when the Like button is pressed |
138 | | `onLoopPress` | Called when the Loop button is pressed |
139 | | `onMutePress` | Called when the Mute button is pressed |
140 | | `onFullscreenPress` | Called when the Fullscreen button is pressed |
141 |
142 |
143 |
144 | ## Custom Buttons
145 |
146 | As illustrated in the sample demo app, the player can be overridden with custom buttons. There is a selection of pre-built action types with their own icons and behaviours or you can add your own with the "custom" action type.
147 |
148 | ```jsx
149 | import { TVPlayer, TVPlayerButtonProps } from "react-tv-player";
150 | import { faGithub } from "@fortawesome/free-brands-svg-icons";
151 |
152 | const customButtons: TVPlayerButtonProps[] = [
153 | { action: "loop", align: "left" },
154 | { action: "like", align: "left" },
155 | { action: "previous", align: "center" },
156 | { action: "playpause", align: "center" },
157 | { action: "next", align: "center" },
158 | { action: "mute", align: "right" },
159 | {
160 | action: "custom",
161 | align: "right",
162 | label: "About",
163 | faIcon: faGithub,
164 | onPress: () => {
165 | window.location.href = "https://github.com/lewhunt/react-tv-player";
166 | },
167 | },
168 | ];
169 |
170 | ;
174 | ```
175 |
176 | | Button Props | Description |
177 | | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
178 | | `action` | Choose from `custom` or one of the pre-built actions: `like`, `loop`, `mute`,`next`,`playpause`,`previous`,`skipforward`, `skipback`, `fullscreen` |
179 | | `align` | Alignment of the button. Choose from `left`,`center`, `right` |
180 | | `label` | A hint text label to appear below the current button in focus. Pre-built button actions use relevent labels. |
181 | | `faIcon` | A font-awesome icon. Pre-built button actions use relevent icons. |
182 | | `onPress` | Called when a button is pressed. Pre-built button actions have their own behaviours. |
183 | | `onRelease` | Called when a button is released. Currently unused. |
184 | | `isSelectedFill` | Allows support of toggle behaviour (in the form of a button fill) when set to true. |
185 | | `disable` | Prevents button action when set to true. |
186 |
187 |
188 |
189 | ## useTVPlayerStore Hook
190 |
191 | For more control you can import the `useTVPlayerStore` custom hook to globally access player state (zustand store). View the sample app and the `TVPlayerUI` inner component for examples of use. Below shows the basics:
192 |
193 | ```jsx
194 | // 1. import useTVPlayerStore
195 | import { TVPlayer, useTVPlayerStore } from "react-tv-player";
196 |
197 | // 2. get state values (there are more availble, see TVPlayerUI.ts for reference)
198 | const actions = useTVPlayerStore((s) => s.actions);
199 | const playing = useTVPlayerStore((s) => s.playing);
200 | const player = useTVPlayerStore((s) => s.player);
201 | const likeToggle = useTVPlayerStore((s) => s.likeToggle);
202 | s;
203 |
204 | const logPlaybackState = () => console.log(playing);
205 |
206 | //3. set state using the actions object
207 | const handleLike = () => {
208 | console.log("like button pressed");
209 | actions.setLikeToggle(!likeToggle);
210 | };
211 |
212 | const togglePlayback = () => {
213 | actions.setPlaying(!playing);
214 | };
215 |
216 | //4. access player instance methods via the player state
217 | const customSeek = () => player.seekTo(player.getCurrentTime() + 10);
218 |
219 | ;
223 | ```
224 |
225 |
226 |
227 | ## Instance Methods
228 |
229 | Use the state's `player` reference - as in the above example - to call instance methods on the player.
230 |
231 | | Method | Description |
232 | | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
233 | | `seekTo(amount, type)` | Seek to the given number of seconds, or fraction if `amount` is between `0` and `1` ◦ `type` parameter lets you specify `'seconds'` or `'fraction'` to override default behaviour |
234 | | `getCurrentTime()` | Returns the number of seconds that have been played ◦ Returns `null` if unavailable |
235 | | `getSecondsLoaded()` | Returns the number of seconds that have been loaded ◦ Returns `null` if unavailable or unsupported |
236 | | `getDuration()` | Returns the duration (in seconds) of the currently playing media ◦ Returns `null` if duration is unavailable |
237 | | `getInternalPlayer()` | Returns the internal player of whatever is currently playing ◦ eg the [YouTube player instance](https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player), or the [`