question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

playing prop not pausing youtube

See original GitHub issue

Be sure to search for your issue before opening a new one.

Current Behavior

When playing prop value changes to false, the video still plays, however if I pause the video manually then the prop changes to true it does start playing So, only the false state is not working as expected

Expected Behavior

When my wrapper component sets the playing state to false the video should stop playing

Steps to Reproduce

Environment

import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n';
import YouTubePlayer from 'react-player/lib/players/YouTube'
import { Waypoint } from 'react-waypoint';

const Youtube = ( { youtubeId } ) => {
	const [ paused, setPaused ] = React.useState( true );

	return (
		<Waypoint
			onLeave={ () => setPaused( true ) }
			onEnter={ () => setPaused( false ) }
		>
			<div>
				{console.log(paused)}
				<YouTubePlayer
					url='https://www.youtube.com/watch?v=YxPRZ9S-qbI'
					controls
					playing={ ! paused }
					// onPause={ () => setPaused( true ) }
					// onPlay={ () => setPaused( false ) }
					// onStart= { () => setPaused( false ) }
					// onReady={ () => setPaused( true ) }
					// onEnded={ () => setPaused( true ) }
				/>
			</div>
		</Waypoint>
	);
};

Youtube.propTypes = {
	youtubeId: PropTypes.string.isRequired,
};

Other Information

I see the playing prop change correctly when scrolling in the React devtools.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pdewouterscommented, Jul 2, 2020

This works, from a stackoverflow answer

	const pauseVideo = () => {
		const videoFrame = ref.current;
		videoFrame && videoFrame.contentWindow.postMessage && videoFrame.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
	};```
1reaction
AlfredoSequeidacommented, Jul 2, 2020

I experienced the same behavior, a way to circumvent this is by getting the internal player instance and then pausing it that way:

this.player.getInternalPlayer().pauseVideo();

Read more comments on GitHub >

github_iconTop Results From Across the Web

playing prop not pausing youtube · Issue #749 - GitHub
When playing prop value changes to false , the video still plays, however if I pause the video manually then the prop changes...
Read more >
How To FIX YouTube Auto Pause Problem
... Pause Problem || YouTube Video Paused. Continue Watching? SOLVEDIsn't it annoying when YouTube pauses the video when it's playing for...
Read more >
Youtube randomly pauses on pc? - Reddit
The only way to resolve this issue is to tap onto another open tab, go back to the tab where the video on...
Read more >
how to play/pause video in React without external library?
So my first suggestion would be to consider refactoring. You can still technically do this with refs by giving your video component a...
Read more >
YouTube Player API Reference for iframe Embeds
This function should be reserved for rare situations when you know that the user will not be watching additional video in the player....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found