add autoplay logic for embedded player (#5399)

Add autoplay logic when player is embedded.

Adds new `autoplay` prop to `VideoJs` component for specifying autoplay value.
This commit is contained in:
Dispatch 2021-01-27 05:49:30 -08:00 committed by GitHub
parent d04b0f09ba
commit 4cf9a455bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 8 deletions

View file

@ -45,6 +45,7 @@ type Props = {
onPlayerReady: Player => void, onPlayerReady: Player => void,
isAudio: boolean, isAudio: boolean,
startMuted: boolean, startMuted: boolean,
autoplay: boolean,
toggleVideoTheaterMode: () => void, toggleVideoTheaterMode: () => void,
}; };
@ -157,20 +158,31 @@ class LbryVolumeBarClass extends videojs.getComponent(VIDEOJS_VOLUME_BAR_CLASS)
properties for this component should be kept to ONLY those that if changed should REQUIRE an entirely new videojs element properties for this component should be kept to ONLY those that if changed should REQUIRE an entirely new videojs element
*/ */
export default React.memo<Props>(function VideoJs(props: Props) { export default React.memo<Props>(function VideoJs(props: Props) {
const { startMuted, source, sourceType, poster, isAudio, onPlayerReady, toggleVideoTheaterMode } = props; const {
autoplay,
startMuted,
source,
sourceType,
poster,
isAudio,
onPlayerReady,
toggleVideoTheaterMode,
} = props;
const [reload, setReload] = useState('initial'); const [reload, setReload] = useState('initial');
let player: ?Player; let player: ?Player;
const containerRef = useRef(); const containerRef = useRef();
const videoJsOptions = { const videoJsOptions = {
...VIDEO_JS_OPTIONS, ...VIDEO_JS_OPTIONS,
autoplay: autoplay,
muted: startMuted,
sources: [ sources: [
{ {
src: source, src: source,
type: sourceType, type: sourceType,
}, },
], ],
autoplay: true,
poster: poster, // thumb looks bad in app, and if autoplay, flashing poster is annoying poster: poster, // thumb looks bad in app, and if autoplay, flashing poster is annoying
plugins: { plugins: {
eventTracking: true, eventTracking: true,
@ -178,8 +190,6 @@ export default React.memo<Props>(function VideoJs(props: Props) {
}, },
}; };
videoJsOptions.muted = startMuted;
const tapToUnmuteRef = useRef(); const tapToUnmuteRef = useRef();
const tapToRetryRef = useRef(); const tapToRetryRef = useRef();

View file

@ -15,6 +15,7 @@ import LoadingScreen from 'component/common/loading-screen';
import { addTheaterModeButton } from './internal/theater-mode'; import { addTheaterModeButton } from './internal/theater-mode';
const PLAY_TIMEOUT_ERROR = 'play_timeout_error'; const PLAY_TIMEOUT_ERROR = 'play_timeout_error';
const PLAY_TIMEOUT_LIMIT = 2000;
type Props = { type Props = {
position: number, position: number,
@ -149,9 +150,7 @@ function VideoViewer(props: Props) {
// https://blog.videojs.com/autoplay-best-practices-with-video-js/#Programmatic-Autoplay-and-Success-Failure-Detection // https://blog.videojs.com/autoplay-best-practices-with-video-js/#Programmatic-Autoplay-and-Success-Failure-Detection
if (shouldPlay) { if (shouldPlay) {
const playPromise = player.play(); const playPromise = player.play();
const timeoutPromise = new Promise((resolve, reject) => { const timeoutPromise = new Promise((resolve, reject) => setTimeout(() => reject(PLAY_TIMEOUT_ERROR), PLAY_TIMEOUT_LIMIT));
setTimeout(() => reject(PLAY_TIMEOUT_ERROR), 2000);
});
Promise.race([playPromise, timeoutPromise]).catch(error => { Promise.race([playPromise, timeoutPromise]).catch(error => {
if (PLAY_TIMEOUT_ERROR) { if (PLAY_TIMEOUT_ERROR) {
@ -221,11 +220,12 @@ function VideoViewer(props: Props) {
<VideoJs <VideoJs
source={source} source={source}
isAudio={isAudio} isAudio={isAudio}
poster={isAudio || (embedded && !autoplayIfEmbedded) ? thumbnail : null} poster={isAudio || (embedded && !autoplayIfEmbedded) ? thumbnail : ''}
sourceType={forcePlayer ? 'video/mp4' : contentType} sourceType={forcePlayer ? 'video/mp4' : contentType}
onPlayerReady={onPlayerReady} onPlayerReady={onPlayerReady}
startMuted={autoplayIfEmbedded} startMuted={autoplayIfEmbedded}
toggleVideoTheaterMode={toggleVideoTheaterMode} toggleVideoTheaterMode={toggleVideoTheaterMode}
autoplay={!embedded || autoplayIfEmbedded}
/> />
</div> </div>
); );