Expo-Camera不支持本地模拟器开发,如何实现功能验证与数据存储?
Expo Camera模拟器录像存储问题的解决方案
所有模拟器均不支持expo-camera的录像功能,在模拟器上开发调用录像接口时会抛出错误:
Possible Unhandled Promise Rejection. Error: Video recording is not supported on a simulator
这会导致功能验证和全流程测试受阻。之前通过判断开发环境替换组件的临时方案,无法完全贴合真实业务逻辑,不是长期最优解。
可行解决方案
方案1:在catch分支模拟返回数据
你想到的在recordAsync的catch分支中模拟录像数据的思路是可行的,建议完善模拟数据的结构,尽量贴合真机返回的字段,避免后续流程因字段缺失报错:
cameraRef?.current .recordAsync(options) .then(vid => { console.log({ vid }); setRecordedVideo(vid); }) .catch(() => { if (process.env.NODE_ENV === 'development') { // 模拟真机返回的完整数据结构 setRecordedVideo({ uri: 'file:///fake-video-path.mp4', width: 1920, height: 1080, duration: 5000, // 模拟5秒视频时长 size: 1024 * 1024 // 模拟1MB文件大小 }); } }) .finally(() => setIsRecording(false));
方案2:提前判断运行环境,避免触发错误
可以通过expo-device的Device.isDevice(或ExponentConstants.isDevice)提前判断是否为模拟器,直接返回模拟数据,无需等待错误抛出,逻辑更顺畅:
import { Device } from 'expo-device'; // ... if (process.env.NODE_ENV === 'development' && !Device.isDevice) { // 模拟器环境直接返回模拟数据 setRecordedVideo({ uri: 'file:///fake-video-path.mp4', width: 1920, height: 1080, duration: 5000, size: 1024 * 1024 }); setIsRecording(false); return; } // 真机环境正常执行录像逻辑 cameraRef?.current .recordAsync(options) .then(vid => { console.log({ vid }); setRecordedVideo(vid); }) .catch(err => { // 处理真机上的真实错误 console.error('录像失败:', err); }) .finally(() => setIsRecording(false));
方案优势
这两种方案都能在模拟器上顺畅走通录像存储的业务流程,同时保留真机环境的正常功能,比替换组件的方式更贴近真实逻辑,减少环境差异带来的测试偏差。
内容的提问来源于stack exchange,提问作者Phil Lucks
相关产品推荐
相关产品推荐

