让expo摄像机参考开始工作,在功能组件中录制视频



我正在尝试让expo相机录制视频。然而,我得到了一个错误:

[TypeError: camera.recordAsync is not a function. (In ‘camera.recordAsync()’, ‘camera.recordAsync’ is undefined)]

这就是实现:

<Camera 
style={{ flex: 1 }} 
type={type}
ref={camera}
>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
}}>
<View style={{ alignSelf: 'flex-end', alignItems: 'center', padding: 20, flexDirection: 'row', justifyContent: 'space-between', width: '100%'}}>
<MaterialIcons name="video-library" onPress={pickVideo} color={"#eee"} size={45}/>     
<RecordingIcon />    
<Ionicons name="ios-reverse-camera" onPress={setCameraType} color={"#eee"} size={45}/>                     
</View>
</View>
</Camera>

重要的一行是RecordingIcon。这表示可以按下以录制和停止录制的图标。

function RecordingIcon (){
if(recording){
stopRecording()
return (
<MaterialIcons name="fiber-manual-record" onPress={() => setRecording(false)} color={"#FF0000"} size={60}/>  
)
} else {
record()
return (
<MaterialIcons name="fiber-manual-record" onPress={() => setRecording(true)} color={"#eee"} size={60}/>  
)
}
}

每次我点击录音图标,就会调用这两个函数中的一个。

async function record(){
console.log("record", camera); 
if(camera){
let recording = await camera.recordAsync(); 
}
}
async function stopRecording(){
console.log("stop recording", camera); 
if(camera){
let stopRecording = await camera.stopRecording(); 
}
}

然而,由于顶部的错误,这两个都不起作用:

async function record(){
console.log("record", camera); 
if(camera){
let recording = await camera.recordAsync(); 
}
}
async function stopRecording(){
console.log("stop recording", camera); 
if(camera){
let stopRecording = await camera.stopRecording(); 
}
}

这就是我初始化相机引用的方式。

let camera = useRef(null);

非常感谢任何人帮助解决此错误。我也试过做camera.current.recordAsync((和camera.ccurrent.stopRecording((,但我得到了同样的错误。

使用camera.current

async function record(){
console.log("record", camera); 
if(camera){
let recording = await camera.current.recordAsync(); 
}
}
async function stopRecording(){
console.log("stop recording", camera); 
if(camera){
let stopRecording = await camera.current.stopRecording(); 
}
}

最新更新