iOS Safari autoplay video
Autoplay muted HTML5 video using React on mobile (Safari / iOS 10+)Jun 20, 2020·3 min read Before you read the rest of the article, I recommend you to try this sandbox on your iOS devices Safari browser you are struggling with. If it works, please do come back! Ive got a tale to tell . https://ney5u.csb.app/ Welcome Back! What a relief! Finally a working solution after spending hours on countless StackOverflow and Github threads. I assure you, you were very close. Without further ado, lets jump to the solution. tag needs all the correct attributes to meet your browsers video autoplay policy requirements. Sounds easy! Modern browsers allow you to autoplay video if it : Only if things were simple! By now most of the browsers will be able to autoplay the video. But Safari needs special care Issue 1: React does not guarantee that muted attribute will be set in the actual DOM and Safari doesnt like that. This React issue is still open as of 20th June 2020. To circumvent this, lets use dangerouslySetInnerHtml to ensure muted attribute is written to the DOM. Issue 2: For iOS 10+ versions, video can only autoplay in fullscreen mode. Read full policy To be able to play inline, webkit requires you to pass playsinline attribute to allow a muted video to autoplay without requiring fullscreen. Lets add that attribute in - Issue 3: Still not able to autoplay videos in some versions of iOS? Lets try some fallback options Till now weve followed all guidelines as per webkit policy. Lets try full-proofing our solution -:
Now lets use this utility in Reacts useEffect and useRefhooks to force play the video on mount. 3. The video.play()promise needs to be handled if it gets rejected. Final output which saves livesHope this article saves you a lot of time. If youre still having trouble with autoplaying muted videos in some Safari versions please leave a comment or DM me on twitter. |