我一直试图让本机相机的视频功能正常工作,但是我已经尝试了大量的方法,但仍然遇到同样的错误。这是我的代码:
class MainCamera extends Component {
constructor() {
super();
this.render = this.render.bind(this)
this.state = { cameraType: Camera.constants.Type.back }
}
render() {
return (
<View style={styles.container}>
<Camera
ref='camera'
style={styles.preview}
aspect={Camera.constants.Aspect.fill}
type={this.state.cameraType}
captureMode={Camera.constants.CaptureMode.video}
captureAudio={false}
target={Camera.constants.CaptureTarget.disk}>
<TouchableHighlight
onPressIn={this.onPressIn.bind(this)}
onPressOut={this.stopVideo.bind(this)}>
<Icon name="video-camera" size={40} />
</TouchableHighlight>
</Camera>
</View>
);
}
onPressIn() {
recordVideo = setTimeout(this.takeVideo.bind(this), 100);
}
takeVideo() {
this.refs.camera.capture({
target: Camera.constants.CaptureTarget.disk
})
.then(data => {
console.log(data);
})
.catch(err => console.log(err));
}
stopVideo() {
this.refs.camera.stopCapture({})
.then(data => console.log(data))
.catch(err => console.log(err));
}
}
当我在stopCapture()方法上使用'.then'保证时,我收到错误“无法读取属性'然后'未定义',但如果我不添加'.then',则没有任何反应我没有收到任何数据。有人有什么建议吗?
答案 0 :(得分:0)
语法错误:
then((data) => {
console.log(data);
})
((data)=>{}) should be done instead of (data=>{}).
答案 1 :(得分:0)
takeVideo() {
this.refs.camera.capture({
audio: true,
mode: Camera.constants.CaptureMode.video,
target: Camera.constants.CaptureTarget.disk
})
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
}
stopVideo() {
this.refs.camera.stopCapture();
}
stopCapture()
函数不是承诺。
答案 2 :(得分:0)
旧文件不幸丢失后的新组件:
class VideoCamera extends Component {
constructor() {
super()
this.state = {
captureMode: Camera.constants.CaptureMode.video,
captureAudio: false,
captureTarget: Camera.constants.CaptureTarget.cameraRoll,
}
}
render() {
return (
<View style={styles.container}>
<Camera
aspect={Camera.constants.Aspect.fill}
captureAudio={this.state.captureAudio}
captureMode={this.state.captureMode}
captureTarget={this.state.captureTarget}
ref="camera"
style={styles.preview}
>
<TouchableHighlight
onPressIn={this._startRecord.bind(this)}
onPressOut={this._endVideo.bind(this)}
>
<Icon
name={'video-camera'}
size={40}
style={styles.recordButton}
/>
</TouchableHighlight>
</Camera>
</View>
)
}
_startRecord() {
startVideo = setTimeout(this._recordVideo.bind(this), 50)
}
_recordVideo() {
this.refs.camera.capture({})
.then((data) => console.log(data))
.catch((err) => console.log(err))
}
_endVideo() {
this.refs.camera.stopCapture()
}
}
答案 3 :(得分:0)
打开相机,创建2个按钮以开始和停止下面的视频。
<View style={styles.container}>
<RNCamera
ref={ref => {
this.camera = ref;
}}
style={styles.preview}
type={RNCamera.Constants.Type.back}
flashMode={RNCamera.Constants.FlashMode.on}
androidCameraPermissionOptions={{
title: 'Permission to use camera',
message: 'We need your permission to use your camera',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}
androidRecordAudioPermissionOptions={{
title: 'Permission to use audio recording',
message: 'We need your permission to use your audio',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}
onGoogleVisionBarcodesDetected={({ barcodes }) => {
console.log(barcodes);
}}
captureAudio={true}
/>
<View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
<TouchableOpacity onPress={this.takeVideo.bind(this)} style={styles.capture}>
<Text style={{ fontSize: 14 }}> VIDEO </Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.stoprec.bind(this)} style={styles.capture}>
<Text style={{ fontSize: 14 }}> STOP </Text>
</TouchableOpacity>
</View>
还创建两种方法来录制视频并停止录制,如下所示。上方按钮中会调用下面的方法。
takeVideo = async () => {
if (this.camera) {
try {
const options = {
quality: 0.5,
videoBitrate: 8000000,
maxDuration: 30
};
const promise = this.camera.recordAsync(options);
if (promise) {
this.setState({ recording: true });
const data = await promise;
this.setState({ recording: false });
}
} catch (error) {
console.log(error);
}
}
}
//stop the recording by below method
stoprec = async () => {
await this.camera.stopRecording();
}
最后,如果您想要文件路径,则所有内容都将作为data.uri
谢谢。希望它能提供清晰的画面