微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在expo中找到录制的视频文件的大小?

如何解决如何在expo中找到录制的视频文件的大小?

我正在与世博会合作。我正在使用世博相机录制视频。我正在获取视频的 URI。但是我想在上传之前计算视频的大小(超过5MB将被禁止)。我只能获取视频的 URI。那么,如何获取视频的大小呢?我也用谷歌搜索过,但没有找到任何相关的答案。

组件文件

import React,{ Component } from "react";
import { View,Text,TouchableOpacity,StyleSheet } from "react-native";
import {
  widthPercentagetoDP as wp,heightPercentagetoDP as hp,} from "react-native-responsive-screen";
import { Camera } from "expo-camera";

export default class Recordvideo extends React.Component {

// Local State
  state = {
    video: null,picture: null,recording: false,hasPermission: null,setHasPermission: "",};

// Getting camera permission
 componentDidMount = async () => {
    const { status } = await Camera.requestPermissionsAsync();
    if (status === "granted") {
      this.setState({ hasPermission: true });
    }
  };

 // Getting Video URI After Recording Completed 
_saveVideo = async () => {
    const { video } = this.state;
    console.log(video);
    this.props.navigation.push("Post",{
      VIDEOURL: video.uri,VIDEOID: 1,mod: true,});
  };

// Stop Recording Function
 _StopRecord = async () => {
    this.setState({ recording: false },() => {
      this.cam.stopRecording();
    });
  };

// Start Recording Function
 _StartRecord = async () => {
    if (this.cam) {
      this.setState({ recording: true },async () => {
        const video = await this.cam.recordAsync();
        this.setState({ video });
      });
    }
  };

// Toogle function for start/stop recording
  toogleRecord = () => {
    const { recording } = this.state;

    if (recording) {
      this._StopRecord();
    } else {
      this._StartRecord();
    }
  };

// Render function
 render() {
    const { recording,video } = this.state;
    if (this.state.hasPermission === null) {
      return <View />;
    }
    if (this.state.hasPermission === false) {
      return (
        <View
          style={{ flex: 1,alignItems: "center",justifyContent: "center" }}
        >
          <Text>No access to camera</Text>
        </View>
      );
    }

   // return statement
    return (
      <View style={styles.responsiveBox}>
        <Camera
          ref={(cam) => (this.cam = cam)}
          style={{
            justifyContent: "flex-end",flex: 1,width: "100%",}}
        >

// on Completing Video button,firing _saveVideo Function
          {video && (
            <TouchableOpacity
              onPress={this._saveVideo}
              style={{
                padding: 20,backgroundColor: "#fff",}}
            >
              <Text style={{ textAlign: "center" }}>Complete</Text>
            </TouchableOpacity>
          )}

// on Toggle VideoRecording for start/stop video recording button,firing _toogleRecord Function
          <TouchableOpacity
            onPress={this.toogleRecord}
            style={{
              padding: 20,backgroundColor: recording ? "#ef4f84" : "#4fef97",}}
          >
            <Text style={{ textAlign: "center" }}>
              {recording ? "Stop" : "Record"}
            </Text>
          </TouchableOpacity>
        </Camera>
      </View>
    );
  }
}


  // Styles
const styles = StyleSheet.create({
  responsiveBox: {
    width: wp("100%"),height: hp("100%"),justifyContent: "center",},});

解决方法

如果你正在使用 expo ,你可以试试 FileSystem

import * as FileSystem from 'expo-file-system';

getFileSize = async fileUri => {
   let fileInfo = await FileSystem.getInfoAsync(fileUri);
   return fileInfo.size;
 };

else 安装 react-native-fs 并使用 stat 方法(获取文件大小)

getFileSize = async fileUri => {
       let fileInfo = await stat(fileUri);
       return fileInfo.size;
     };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。