Reputation: 47
Using React JS, I'm trying to make a website similar to Tiktok, where on scroll, the video below it will play (with AUDIO, I know it's much easier without).
I tried to set this up with a window.addEventListener("scroll",...)
set up. But on further research it seems like this doesn't work because the scroll isn't a "valid user input" to allow playing a video on mobile.
After more research it seems like playing then pausing all the videos from an initial user input click would work. Though I'm still working on how to play/pause all the videos from that single input.
Anyway, my question is how do websites like tiktok.com and youtube shorts autoplay videos with sound upon scrolling? How do they accomplish it?
Upvotes: 3
Views: 4046
Reputation: 169
I am sure there are many ways to do it.
One way I would do is to use Intersection Observer API
You can trigger some callback everytime you enter the view of the of
as per docs some usecases
The Intersection Observer API allows you to configure a callback that is called when either of these circumstances occurs:
let options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
let observer = new IntersectionObserver(callback, options);
// or
var intersectionObserver = new IntersectionObserver(function(entries) {
// If intersectionRatio is 0, the target is out of view
// and we do not need to do anything.
if (entries[0].intersectionRatio <= 0) return;
loadItems(10);
console.log('Loaded new items');
});
// start observing
intersectionObserver.observe(document.querySelector('.scrollerFooter'));
I had made a small npm package for doing lazy loading.
https://www.npmjs.com/package/dyno-img-react
It basically shows a low res image till a user reaches the element ( I can configure the intersection area ). Once it reaches, I would fetch the high-quality image, and remove the observer
If its for RN, I have not really worked recently on RN, but you can check out this library
react-native-intersection-observer
It has an implementation which you can see in the code here.
https://github.com/zhbhun/react-native-intersection-observer/blob/master/src/IntersectionObserver.ts
It uses onLayout event to manage it internally
import { IOScrollView, InView } from 'react-native-intersection-observer'
const Component = () => (
<IOScrollView>
<InView onChange={(inView: boolean) => console.log('Inview:', inView)}>
<Text>Plain children are always rendered. Use onChange to monitor state.</Text>
</InView>
</IOScrollView>
)
export default Component
Upvotes: 1
Reputation: 85575
Check this guide for autoplay availibility:
Otherwise, the playback will likely be blocked. The exact situations that result in blocking, and the specifics of how sites become whitelisted vary from browser to browser, but the above are good guidelines to go by.
To allow autoplay feature, you have to force your users by configuring options to allow. Here's a feature policy that you can try with in header:
Feature-Policy: autoplay
Allowing autoplay in iframe:
<iframe src="mediaplayer.html"
allow="autoplay 'src'">
</iframe>
But browsers block autoplay with audio as their policy - user can feel irritation with the sound that is automatically appear. So, all thing you have to set a configuration for them to allow to play with sound.
For full information about autoplay, follow the guide. You can also check this tool to see what devices are allowed for autoplay along with autoplay policy.
Upvotes: 0