You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在expo-av中使用本地文件路径播放iOS模拟器视频?

在Expo AV中使用本地文件路径播放视频的解决方案

问题说明

你尝试直接用iOS模拟器的绝对路径通过require()加载视频,但这种方式无法生效,需要采用正确的本地文件加载逻辑。

正确实现方式

Expo AV的Video组件支持播放本地文件,但不能用require()加载设备绝对路径——require()仅适用于项目打包时就包含的静态资源,运行时生成或存储的本地文件需要用uri格式的source。

1. 直接使用已知绝对路径播放

将source改为{ uri: '你的文件路径' }格式,替换你之前的require()写法:

import { Video } from 'expo-av';

// 替换为你的本地视频绝对路径
const localVideoPath = '/Users/jamess/Library/Developer/CoreSimulator/Devices/6B345354-6F3456-4535A-5353454-9814353456F/data/Containers/Data/Application/D34535D5-E7A5-4F35354F-B42342B-7223424535352/Documents/video5_g243f4.mp4';

export default function App() {
  return (
    <Video
      source={{ uri: localVideoPath }}
      resizeMode="cover"
      repeat
      shouldPlay
      style={styles.backgroundVdeo}
    />
  );
}

2. 通用适配方案(推荐)

iOS模拟器/真机的应用容器ID会随重装变化,硬编码绝对路径不适合长期使用。可以借助expo-file-system动态获取Documents目录,再拼接文件名:
首先安装依赖:

npx expo install expo-file-system

然后编写代码:

import { Video } from 'expo-av';
import * as FileSystem from 'expo-file-system';

export default function App() {
  // 动态获取Documents目录路径
  const docDir = FileSystem.documentDirectory;
  // 拼接视频文件名
  const videoUri = docDir + 'video5_g243f4.mp4';

  return (
    <Video
      source={{ uri: videoUri }}
      resizeMode="cover"
      repeat
      shouldPlay
      style={styles.backgroundVdeo}
    />
  );
}

关键注意事项

  • require()仅能加载项目根目录下的静态资源,无法访问设备上的任意绝对路径文件。
  • 使用uri格式时,确保目标文件存在,且应用拥有访问权限(Documents目录默认开放访问)。

内容的提问来源于stack exchange,提问作者James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 16:03:25