前端中怎么自动播放mov格式视频?实例代码分享!

我的夏天来了 2021-08-10 16:04:13 浏览数 (4395)
反馈

对于视频自动播放大家应该都不陌生那么今天我们来说说:“前端中怎么自动播放mov格式视频?”这个问题的方法和相关的解决内容吧! 

这个问题应该这么看。

1、首先网站要支持.MOV格式文件

就是说,网站要能识别.MOV格式文件。

<mimeMap fileExtension=".mov" mimeType="video/quicktime" />

如何识别?设置MIME类型。以IIS为例。除了可以在IIS界面上直接设置,还可以在项目的web.config里设置。给个完整的例子

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <defaultDocument>
            <files>
                <remove value="default.aspx" />
                <remove value="iisstart.htm" />
                <remove value="index.html" />
                <remove value="index.htm" />
                <remove value="Default.asp" />
                <remove value="Default.htm" />
            </files>
        </defaultDocument>
        <staticContent>
   <remove fileExtension=".mp4" />
   <remove fileExtension=".wasm" />
   <remove fileExtension=".woff" />
   <remove fileExtension=".woff2" />
   <remove fileExtension=".mov" />
    
   <mimeMap fileExtension=".mp4" mimeType="video/mpeg" />
            <mimeMap fileExtension=".wasm" mimeType="application/wasm" />
            <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
            <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
   <mimeMap fileExtension=".mov" mimeType="video/quicktime" />
        </staticContent>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
            </customHeaders>
        </httpProtocol>
        <caching>
            <profiles>
                <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
                <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
                <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
                <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
            </profiles>
        </caching>
    </system.webServer>
</configuration>

2、HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body,center{
padding:0;
margin:0;
}
</style>
</head>
<body>
  <center>
  <video id="video"  width="640" height="480" muted controls autoplay="autoplay" preload="auto" >
    <source src="月半湾.mov" />
    您的浏览器不支持 HTML5 video 标签。
  </video>
  </center>
</body>
</html>

本例会自动播放。自动播放的关键是“muted”属性(静音),否则无论是声明autoplay=“autoplay”,还是用脚本video.play()都不起作用。这个应该是故意设计成这样的。否则,打开就自动播放,万一是爱情动作片怎么办?如果静音就少了许多顾虑。

总结

对于:“前端中怎么自动播放mov格式视频? ”这个问题,我们的解决方法和相关内容都在文章中可以了解到! 希望对大家有所帮助,更多的内容我们可以在W3Cschool中进行学习和了解! 

0 人点赞