简体   繁体   English

react material-ui CardMedia 组件中的自动播放视频

[英]Autoplay video in react material-ui CardMedia component

I have a Material UI Card component that I'd like to have contain a video (webm) that autoplays.我有一个Material UI Card 组件,我希望它包含一个自动播放的视频 (webm)。 (It's a mute video) (这是一个静音视频)

The documentation for the CardMedia component indicates to list any HTML element as the component element type -- so I have listed "video". CardMedia 组件的文档指示将任何 HTML 元素列为组件元素类型——所以我列出了“视频”。

I have tried playing with suggestions from here: Video autoplay not working - Trying to find a fix我已经尝试使用这里的建议: 视频自动播放不起作用 - 试图找到修复

Specifically, I've tried added component: 'video autoPlay muted to no avail - getting an error that the element cannot be created.具体来说,我已经尝试添加component: 'video autoPlay muted无济于事 - 收到无法创建元素的错误。 I also tried passing an actual tag: <video autoPlay muted> ... also failure ('expecting string').我还尝试传递一个实际标签: <video autoPlay muted> ...也失败('expecting string')。

<Card className={classes.root} raised={true}>
        <CardHeader
            title={camera.cameraName}
        />

        <CardActionArea>
            <CardMedia
                component='video'
                className={classes.media}
                image={"path/to/file/video.webm"}
            />
            <CardContent>

                <Typography variant="body2" color="textSecondary" component="p">
                    Some Text
                </Typography>
            </CardContent>
        </CardActionArea>
    </Card>

Any suggestions on how I can get a video to auto play in a MaterialUI CardMedia component?关于如何在 MaterialUI CardMedia 组件中自动播放视频的任何建议?

Alternatively, I'd settle for simply having the controls show up by default - as is, you have to right click and select 'show controls'.或者,我会满足于简单地默认显示控件 - 照原样,您必须右键单击并选择“显示控件”。

React Material UI CardMedia video component not playing led me to the idea that CardMedia appears to simply pass on properties to the 'parent' component (a tag, in this case) React Material UI CardMedia 视频组件未播放让我想到 CardMedia 似乎只是将属性传递给“父”组件(在本例中为标签)

As such, I was able to add an 'autoPlay' property to the CardMedia and it worked.因此,我能够向 CardMedia 添加一个“自动播放”属性并且它起作用了。

        <CardMedia
            component='video'
            className={classes.media}
            image={"path/to/file/video.webm"}
            autoPlay
        />

Note that adding 'controls' also added controls to the video.请注意,添加“控件”也会向视频添加控件。

此外,如果在使用 iframe 组件时对其他人有帮助,您可以在 URL 中设置allow="autoPlay"?autoplay=1&mute结合使用,它会起作用:

<CardMedia component="iframe" src={video} allow="autoPlay"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM