Mobile browser experiences video freezing when src is set through useState, yet it starts playing when triggered by a button click or when the video is set to M

Hey awesome folks at the StackOverflow Community,

I'm currently encountering a perplexing issue with a video element in my web application. The video is supposed to play automatically on both desktop and mobile browsers. However, I've run into a specific problem on mobile browsers and PWAs where the video freezes when avatarVideoLink is set to a URL generated from a response (a Blob URL). Strangely enough, this issue doesn't manifest on desktop browsers - the video plays as intended either when the Talk button is clicked or if the video is muted.

Below is the pertinent segment of my code:

const res = await response;
if (res.body instanceof ReadableStream) {
  let responseStream = await new Response(res.body);
  let arrayBuffer = await responseStream.arrayBuffer();
  let blob = new Blob([arrayBuffer], { type: "video/mp4" });
  let url = URL.createObjectURL(blob);
  setAvatarVideoLink(url);
} else {
  setAvatarVideoLink(avatarIdleVideoUrl);
}

// Video component
<video
  key={avatarVideoLink}
  ref={videoRef}
  src={avatarVideoLink}
  autoPlay
  playsInline
  loop={avatarVideoLink === avatarIdleVideoUrl}
  style={{ height: "60vh", borderRadius: "3em", objectFit: "cover" }}
/>

// Button component
<Button
  onClick={() => {
    if (listening) {
      SpeechRecognition.stopListening();
      generateOpenAIResponse(text, true);
    } else {
      resetTranscript();
      setText("");
      SpeechRecognition.startListening({ continuous: true });
    }
  }}
  type={"primary"}
  danger
  loading={avatarIsLoading}
>
  {avatarIsLoading ? avatarStatus : listening ? "Send 📩" : "Talk 🎤"}
</Button>

Observed Behavior:

Desktop browsers: The video autoplays without any issues. Mobile browsers: The video freezes initially but resumes when the Talk button is clicked or if the video is muted. Despite checking the video file for problems, everything seems fine. My suspicion lies with potential mobile browser policies concerning autoplay videos, though I can't be certain.

Inquiries:

Why does the video freeze exclusively on mobile browsers when the src is set to the dynamically generated URL? Are there workarounds available to ensure automatic playback of the video on mobile browsers similar to desktop browsers? I'd greatly appreciate any insights or suggestions to help resolve this concern. Thank you all in advance for your assistance!

Upon altering the useState for avatarVideoLink, the video should kick off automatically.

The end result is that the video loads but pauses, requiring manual interaction through a button click or muting the component for autoplay functionality.

Answer â„–1

Apple has implemented a security measure in WebKit to prevent auto-playing of media elements on Safari.

Safari's automatic inference engine in macOS High Sierra blocks auto-play for media elements with sound on most websites by default.

Websites should require user interaction such as a gesture click to play any audio or video content.

Check out these related posts HTML5 Video autoplay on iPhone

tldr; To avoid auto-play issues, consider starting the video muted and/or prompting user interaction before playing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementation of Exporting to Excel

function fnshowAuditList() { if(auditListTable) auditListTable.fnDestroy(); jQuery.ajax({ type: 'POST', url: 'auditListAction', data: '', dataType: 'text&a ...

Utilizing the power of JavaScript, CSS, and HTML to seamlessly transfer selected items from one webpage to the shopping cart displayed on another page

Due to limitations on using back-end code, I'm seeking advice on how to implement this function. Most tutorials I've come across show the items and shopping cart on the same page. I've heard suggestions about using Jquery.load(), but I&apos ...

The form validation feature in NestJS using Class Validator appears to be malfunctioning

Hey everyone, I've been working on validating incoming form data using the class validator package and DTO. Here's my DTO: import { IsString, IsPhoneNumber, IsEnum, MinLength } from 'class-validator'; export class CreateAgentDto { @ ...

What is the best way to set the theme for Material-UI in a React application?

I find myself puzzled when it comes to styling a front-end based on Material-UI. Can someone clarify where the theme originates from in the following example code and what impact the theme has? import React from "react"; import Container from "@material- ...

Utilizing $.getJSON to initiate a selection change event

I'm currently working on implementing a feature that involves adding categories to a dropdown list using jQuery Ajax. The goal is to load subcategories when a particular option is selected. However, I've encountered an issue where the addition o ...

When using jQuery and Laravel, why does the element not appear when setting its display to block after receiving a response?

Trying to handle data (id) retrieved from the database and stored in a button, which appears in a modal like this: There are buttons for "Add" and "Remove", but the "Remove" button is hidden. What I want to achieve: When the user clicks on the "Add" but ...

Handling events for components that receive props from various components in a list

In my code, I have a component called PrivateReview which includes event handlers for updating its content. export default function PrivateReview(props) { const classes = useStyles(); const removeReviewAndReload = async () => { await ...

Prevent the execution of a Javascript function if it is already in progress

I've developed a function that retrieves records from a third party, and this function runs every 10 seconds. However, as I debug through Firefox, I notice a long queue of ajax requests. I'm contemplating including a statement that can signal to ...

Ways to resolve the issue of data-bs-target not functioning properly in Bootstrap version 5

While viewing the Job screen in the following image, I attempted to click on "Personal," but it remained stuck on the Job screen. ...

Check the feature that retrieves data from a `json` file

In my util file, I have a function that imports and checks whether a given sectionUUID has a video in the JSON file. import multipleVideos from '../data/videos.json' function hasSectionMultipleVideos (sectionUUID) { return multipleVideos.vide ...

Using JavaScript regular expressions for email validation criteria

Hey there, I am struggling with Regular Expressions, especially when it comes to client side validation for a specific field. Can you please help me come up with a Regular Expression that would verify if an email address is valid based on these criteria: ...

The makeStyles feature is currently not functioning properly in the latest version of Next.js with Material UI v5

Currently, I am utilizing nextjs along with material ui in my project "@mui/material": "^5.0.1", "@mui/styles": "^5.0.1", Below is the code snippet of my component structure import { AppBar, Toolbar, Typography, Box ...

A guide on utilizing the context API consumer within the getInitialProps function of Next.js

Is there a way to incorporate the context API within getInitialProps in order to manage the loading spinner while making requests? What is the best approach to invoke my context methods within getInitialProps function? ...

Unexpected behavior observed with callback function when inserting a query in Node.js

Having a minor issue with using the POST method and adding an INSERT. The functionality works correctly as shown below, but I am looking to implement a callback after the data has been inserted. Currently, the database is updated successfully, but I am una ...

Can you please provide the appropriate PropTypes for a dictionary in a ReactJS project?

Looking for something along the lines of y = {"key0": [value0, value1], "key1":[value2]} What is the proper way to define the proptypes for y? ...

Utilize the parsing functionality in three.js to extract JSON geometry data

After exporting a model from Blender using the three.js exporter and successfully loading it with the JSONLoader, my next challenge is to store the JSON information in a variable and parse it to display the model without having to load an external file. T ...

Using AngularJS to show/hide elements within a colgroup tag

Looking to create a dynamic table allowing users to hide certain columns. Wondering if it's possible to use ng-show with colgroup or col tags instead of adding ngshow to each cell individually. Struggling to make it work... <colgroup ng-repeat="mt ...

Trouble getting static files from `public` directory to load properly on custom NextJS server with ExpressJS

I've been working on my NextJS app and have set up a custom server using ExpressJS. However, I'm facing an issue where the app is unable to locate the static files within the public folder when running with the custom server. It's worth n ...

I have my server running on port 6666. I am able to receive a response from Postman, however, when I attempt to access localhost:6666 in my browser, it displays a message

[image description for first image][1] [image description for second image][2] [image description for third image][3] There are three images displayed, indicating that the server is operational and responding with "hello" in Postman, but there seems to ...

Implement a personalized loading spinner in your Next.js application

What is the process for implementing a custom loader in Next.js instead of using the default option? const loading = true; if (loading) { return <CustomLoading />} ...