Mobile Safari 不显示我的视频纹理

发布于 2025-01-15 01:44:15 字数 1433 浏览 2 评论 0原文

我有一个类似于这个的问题,但就我而言,它是iOS造成麻烦(不是 macOS,我还没有尝试过),所以我希望也可以发布这个。我尝试在 Three.js 中创建视频纹理,但无法使其在移动 Safari (iOS 15.4) 上运行。这是我的代码,我试图尽可能地整理它:


import React, { useEffect, useRef } from "react";
import * as THREE from "three";
import { Canvas } from "@react-three/fiber";

import "./styles.css";

const Screen = () => {
  const meshRef = useRef();
  useEffect(() => {
    const vid = document.createElement("video");
    vid.src = "/test.mp4";
    vid.crossOrigin = "Anonymous";
    vid.loop = vid.muted = vid.playsInline = true;
    vid.play();
    meshRef.current.material.map = new THREE.VideoTexture(vid);
  });
  return (
    <mesh ref={meshRef}>
      <planeGeometry attach="geometry" />
    </mesh>
  );
};
const App = () => {
  return (
    <Canvas camera={{ fov: 25 }}>
      <Screen />
    </Canvas>
  );
};

export default App;

请告诉我我在这里做错了什么。 test.mp4 来自 此网址。我还尝试将视频放置为 HTML 元素,而不是动态创建它,然后视频本身可以正常播放,但视频纹理不能播放。

另外,只是好奇,但为什么在主组件的 useEffect 中不可用 meshRef.current ,但在 内部的 useEffect 可用Screen,放在Canvas里面,可以吗?

I have a question similar to this one, but in my case, it's iOS causing troubles (not macOS, which I haven't tried yet), so I hope it's OK to post this as well. I tried to create a video texture in Three.js and can't bring it to work on mobile Safari (iOS 15.4). Here is my code, which I tried to tidy up as much as possible:


import React, { useEffect, useRef } from "react";
import * as THREE from "three";
import { Canvas } from "@react-three/fiber";

import "./styles.css";

const Screen = () => {
  const meshRef = useRef();
  useEffect(() => {
    const vid = document.createElement("video");
    vid.src = "/test.mp4";
    vid.crossOrigin = "Anonymous";
    vid.loop = vid.muted = vid.playsInline = true;
    vid.play();
    meshRef.current.material.map = new THREE.VideoTexture(vid);
  });
  return (
    <mesh ref={meshRef}>
      <planeGeometry attach="geometry" />
    </mesh>
  );
};
const App = () => {
  return (
    <Canvas camera={{ fov: 25 }}>
      <Screen />
    </Canvas>
  );
};

export default App;

Please tell me if I'm doing something wrong here. The test.mp4 is from this URL. I also tried to place the video as HTML element, instead of creating it dynamically, then the video itself plays fine, but not the video texture.

Also, just curious, but why isn't meshRef.current available in a useEffect in the main component, but useEffect inside of Screen, which is placed inside of Canvas, is OK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南…巷孤猫 2025-01-22 01:44:15

显然这是视频文件格式的问题。尝试了来自 Three.js 的示例视频,它成功了。

Apparently it's a problem with video file formats. Tried an example video from Three.js and it worked.

最单纯的乌龟 2025-01-22 01:44:15

对于那些寻找解决方案的人来说,您需要添加

vid.playsInline=true; 

移动 iOS 设备。

To those of you looking for the solution , you need to add

vid.playsInline=true; 

for mobile ios devices.

留一抹残留的笑 2025-01-22 01:44:15

我也有同样的问题。我必须以非常具体的方式设置“playsinline”属性。

video.playsinline= true 不起作用,但 video.setAttribute('playsinline', true)
做了工作。

希望这有帮助

I had the same problem. I had to set the 'playsinline' attribute in a very specific way.

video.playsinline= true did not work but video.setAttribute('playsinline', true)
did work.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文