瀏覽代碼

safari autoplay

master
azri 1 周之前
父節點
當前提交
de6ece57b5
共有 1 個檔案被更改,包括 10 行新增1 行删除
  1. 10
    1
      src/components/VideoAds/VideoAds.jsx

+ 10
- 1
src/components/VideoAds/VideoAds.jsx 查看文件

@@ -1,4 +1,4 @@
1
-import React, { useRef, useState } from 'react';
1
+import { useRef, useState, useEffect  } from 'react';
2 2
 import { Box, Button } from '@mui/material';
3 3
 import PropTypes from 'prop-types';
4 4
 import videoAds from "../../assets/video/amberads.mp4";
@@ -7,6 +7,15 @@ const VideoAds = ({ video_url, height, width }) => {
7 7
   const videoRef = useRef(null);
8 8
   const [isMuted, setIsMuted] = useState(true);
9 9
 
10
+  useEffect(() => {
11
+    if (videoRef.current) {
12
+      videoRef.current.muted = true; // Ensure video starts muted for autoplay
13
+      videoRef.current.play().catch((err) => {
14
+        console.warn('Autoplay failed:', err);
15
+      });
16
+    }
17
+  }, []);
18
+
10 19
   const toggleMute = () => {
11 20
     if (videoRef.current) {
12 21
       videoRef.current.muted = !isMuted;

Loading…
取消
儲存