Mobile Safari 不显示我的视频纹理
我有一个类似于这个的问题,但就我而言,它是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
可用ScreenCanvas
里面,可以吗?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然这是视频文件格式的问题。尝试了来自 Three.js 的示例视频,它成功了。
Apparently it's a problem with video file formats. Tried an example video from Three.js and it worked.
对于那些寻找解决方案的人来说,您需要添加
移动 iOS 设备。
To those of you looking for the solution , you need to add
for mobile ios devices.
我也有同样的问题。我必须以非常具体的方式设置“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 butvideo.setAttribute('playsinline', true)
did work.
Hope this helps