如何使用react-native-camera模块捕获视频?

时间:2018-02-06 07:25:23

标签: reactjs react-native

我可以毫无问题地使用此模块捕捉图片,但是当我尝试录制视频时,我似乎无法找到录制视频的位置(或者甚至根本没有录制视频)。这是我的CameraScreen

import Camera from 'react-native-camera';

const { CaptureMode, CaptureTarget } = Camera.constants;
const { video: captureModeVideo, still: captureModePhoto } = CaptureMode;

class CameraScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            captureMode: captureModePhoto,
            isRecording: false
        };

        this.onCapture = this.onCapture.bind(this);
        this.onSwitchCaptureMode = this.onSwitchCameraMode.bind(this);
    }

    onCapture() {
        const { captureMode, isRecording } = this.state;

        if (isRecording) {
            this._camera.stopCapture();
            this.setState({ isRecording: false });
            return;
        }

        if (captureMode === captureModeVideo) {
            this.setState({ isRecording: true });
        }

        this._camera.capture({ mode: captureMode })
            .then((result) => console.log(result))
            .catch((error) => console.log(error));
    }

    onSwitchCaptureMode() {
        if (this.state.captureMode === captureModeVideo) {
            this.setState({ captureMode: captureModePhoto });
        } else {
            this.setState({ captureMode: captureModeVideo });
        }
    }

    render() {
        const { captureMode } = this.state;

        return (
            <Camera
                ref={(ref) => this._camera = ref}
                style={{ flex: 1 }}
                captureMode={captureMode}
                captureTarget={CaptureTarget.disk}
            >
                <TouchableOpacity onPress={this.onCapture}>
                    <Icon
                        name='camera-alt'
                        ...
                        ...
                    />
                </TouchableOpacity>
                <TouchableOpacity onPress={this.onSwitchCaptureMode}>
                    <Icon
                        name='...'
                        ...
                        ...
                    />
                </TouchableOpacity>
            </Camera>
        );
    }
}

export default CameraScreen;

当我拍照时,console.log(result)语句记录照片的路径没有问题,但是当captureMode === captureModePhoto,我的调试器中没有任何日志时,有什么我的意思吗?做错了?我省略了许多样式以使代码更容易理解

1 个答案:

答案 0 :(得分:0)

这是一个愚蠢的错误

我发布的代码绝对正常,但我必须在真实的​​设备上测试它。模拟器支持拍摄图像(它生成一些具有随机背景颜色的图像),但不支持拍摄视频。因此,问题解决了