react icon indicating copy to clipboard operation
react copied to clipboard

HTML5 video tag support muted parameter

Open cometta opened this issue 10 years ago • 51 comments

I am unable to use muted parameter for <video autoplay muted loop>.. . Other parameters for video seems fine.

cometta avatar Apr 19 '16 03:04 cometta

Appears to work fine for me: http://jsfiddle.net/314xxopf/

jimfb avatar Apr 19 '16 05:04 jimfb

@cometta, which browser you are using?

GTVanitha avatar Apr 19 '16 05:04 GTVanitha

you are right. i retested it. sorry. I proceed to close it.

cometta avatar Apr 19 '16 05:04 cometta

Strangely enough, I ran into the same issue today, on different Chrome versions. I tested with the fiddle above and the muted attribute is unavailable:

screen shot 2016-05-09 at 14 09 08 screen shot 2016-05-09 at 14 10 41

moret avatar May 09 '16 17:05 moret

I expanded a bit @jimfb's fiddle at http://jsfiddle.net/wmzjzjmm/ to check server rendering and tested in Safari as well. renderToString rendered both muted and controls attributes, despite only the controls attribute showing up when using render: screen shot 2016-05-09 at 14 42 41 screen shot 2016-05-09 at 14 43 43

The video is muted despite the attribute not showing up, but I can only access the attribute when I'm resuming a server render.

moret avatar May 09 '16 17:05 moret

@moret When I run the code in your fiddle, the video appears to be muted (no sound). That's the only thing that React guarantees; React is not required to set the attribute if it is not required.

jimfb avatar May 09 '16 20:05 jimfb

When rendering on the client we'll use the property since it was determined that modifying the attribute didn't actually affect the video. We must set it as node.muted = true|false, so you don't see that reflected in the attribute.

zpao avatar May 09 '16 20:05 zpao

@zpao makes sense, and that explains well the difference between the seemingly similar attributes. Thanks for the explanation!

moret avatar May 09 '16 20:05 moret

@zpao ohai, and also it seems that Mobile Safari in iOS10 uses the video's muted attribute to determine if the video can autoplay: https://webkit.org/blog/6784/new-video-policies-for-ios; having video.muted == true isn't enough.

aza avatar Nov 07 '16 23:11 aza

I'd argue this needs to be reopened. This breaks iOS10 autoplay support.

lawlmart avatar Nov 17 '16 20:11 lawlmart

<video muted> elements will also be allowed to autoplay without a user gesture.

and

<video> elements will be allowed to play() without a user gesture if their source media contains no audio tracks, or if their muted property is set to true.

These conflicting statements lead me to believe we can (and probably should) continue using the property. We might just need to ensure that muted is set before autoplay.


Can somebody having an issue share the code they're having an issue with (hi @aza!) - I can't repro the problem but I could be looking at the wrong thing. Here's what I have:

ReactDOM.render(
  <video width="100%" controls autoPlay muted playsInline>
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4" />
  </video>,
  document.getElementById('container')
);

This is rendering the video and autoplaying inline without any issue (iOS 10.1 in the simulator with React 15.4). If I leave off muted then it will not autoplay.

zpao avatar Nov 18 '16 00:11 zpao

I have the same issue: react 15.4.2 + SSR

mschipperheyn avatar Mar 14 '17 00:03 mschipperheyn

I'm (begrudgingly) working around this issue by using dangerouslySetInnerHTML:

const VideoWorkaround = ({ src }) => (
  <div dangerouslySetInnerHTML={{ __html: `
    <video
      muted
      autoplay
      playsinline
      src="${src}"
    />
  ` }}
  />
);

This issue should absolutely be reopened.

kohlmannj avatar Apr 12 '17 18:04 kohlmannj

this is quite odd.

With both the regular JSX syntax and the template-string version posted by @kohlmannj, I'm seeing the muted attribute stripped off of the node. All other attributes are kept in tact.

      <video
        muted
        playsInline
        preload
        loop
        autoPlay
      >
        <source src={this.state.src} type="video/mp4" />
      </video>

Any ideas what is going on here? This seems to work ok in iOS10, however Android seems to drop the muted attribute and just not play.

if I try to invoke play ie this.elementRef.play(), I get the must be initiated as a result of user interaction error.

Emulated Pixel on Android 7, mobile Chrome v.51.0.02704.90

tconroy avatar Apr 26 '17 19:04 tconroy

Has anyone gotten autoplay video to work on Android with React?

Seems to work on iOS 10 with playsInline + autoPlay but cannot get it to work consistently on Android. Is this limited by Android version?

tconroy avatar May 23 '17 19:05 tconroy

This is still an issue. Here was my original code:

const Video = ({ src, onEnded }) => (
  <video src={src} autoPlay playsInLine muted onEnded={onEnded} ref="video"/>
)

It would load videos but wouldn't play them on iOS.

I fixed it by doing this, but it produces a laggy experience because the video doesn't play immediately.

class Video extends Component {
    componentDidMount() { this.update(); }

    componentDidUpdate() { this.update(); }

    update() {
        this.refs.video.setAttribute('muted', '1');
        this.refs.video.setAttribute('playsinline', '1');
        this.refs.video.setAttribute('autoplay', '1');
    }

    render() {
        const { src, onEnded } = this.props;

        return (
            <video src={src} autoPlay playsInLine muted onEnded={onEnded} ref="video"/>
        )
    }
};

You can see the difference by going here on both iOS and Desktop. I haven't tried on Android.

Swizec avatar Oct 02 '17 01:10 Swizec

I made a package to render a video tag using dangerouslySetInnerHTML.

It's pretty specific to my use case of animated gif like videos but feel free to send a PR or open an issue if you want something else.

https://github.com/DylanVann/react-video-tag

DylanVann avatar Jul 20 '18 01:07 DylanVann

I enabled autoplay on iOS and React 16 with the videoelement defaultMuted property: https://gist.github.com/diuis/fc1c2f0e255538cda397c5a6b669c9bf

diuis avatar Oct 05 '18 13:10 diuis

Um. We need the muted attribute in the HTML.

No limitation on autoplay muted video https://medium.com/@sundaykuloksun/video-play-not-working-html5-autoplay-policy-beed81d64ca5

jpdevries avatar Oct 11 '18 21:10 jpdevries

Thanks @kohlmannj that worked for me. This issue needs to be fixed though, it's well documented that muted is required for auto playback on iOS.

lewisdonovan avatar Oct 18 '18 13:10 lewisdonovan