【问题标题】:Retrieve fileSize and playableDuration of Video from React-native cameraRoll library从 React-native cameraRoll 库中检索视频的 fileSize 和 playableDuration
【发布时间】:2020-07-15 22:32:28
【问题描述】:

我正在尝试使用适用于 React 本机的 cameraRoll 库打印每个视频的总持续时间和视频大小,但它对所有视频都显示为空。 这可以修复吗,所以我可以显示每个视频的总时长和视频大小。

CameraRoll 库链接https://github.com/react-native-community/react-native-cameraroll

export default class camera extends Component {
  constructor(props) {
    super(props);
    this.state = {
        data:''
    };
  }

  async componentDidMount()
  {
    if (Platform.OS === 'android') {
        const result = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
          {
            title: 'Permission Explanation',
            message: 'ReactNativeForYou would like to access your photos!',
          },
        );
        if (result !== 'granted') {
          console.log('Access to pictures was denied');
          return;
        }
      }

      CameraRoll.getPhotos({
        first: 3,
        assetType: 'Videos',
      })
      .then(res => {
        this.setState({ 
          data: res.edges,
                    
        });
      })
      .catch((error) => {
         console.log(error);
      });
    
  }


  render() {
    return (
      <View style={{flex: 1, flexDirection: 'row'}}>
        <FlatList
        
        data={this.state.data}
        numColumns={1}
        renderItem={({ item }) => 

          <View 
          style={{    flexDirection: 'row',}}
          >     

           <Video    
           ref={(ref) => {
             this.player = ref
           }}   
           source={{ uri: item.node.image.uri }}                                  
           onBuffer={this.onBuffer}                
           onError={this.videoError}  
           resizeMode='cover'
           repeat={true}
           muted={true}
           playWhenInactive={true}            
           style={{
            width: '30%',
            height: 80,
            marginLeft:10,
            marginTop:10,
            borderRadius:10,
          }} 
          />

           <Text 
          style={{textAlign: 'center', alignSelf: 'stretch', textAlignVertical: 'center', 
                  paddingLeft: 30, overflow: "hidden", }}
          >{JSON.stringify(item.node.image.fileSize)}    // this line should have displayed the video file size
          </Text> 

          <Text 
          style={{textAlign: 'center', alignSelf: 'stretch', textAlignVertical: 'center', 
                  paddingLeft: 30, overflow: "hidden", }}
          >{item.node.image.playableDuration}    //This line should have displayed the video duration
          </Text>


          </View>
          }
      />
      </View>
    );
  }
}

这是我的完整代码。

【问题讨论】:

    标签: reactjs react-native react-native-android react-native-camera react-native-video


    【解决方案1】:
    CameraRoll.getPhotos({
        first: 3,
        assetType: 'Videos',
        includes: ['fileSize', 'playableDuration']
    });
    

    【讨论】:

      【解决方案2】:

      这可能是由于 includeFileName 和 includeFileSize 变量为 false。 因此,要包含它们,请在以下文件路径中进行更改:node_modules/@react-native-community/cameraroll/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java

      变化:

      // if (includeFilename) {
      //   File file = new File(media.getString(dataIndex));
      //   String strFileName = file.getName();
      //   image.putString("filename", strFileName);
      // } else {
      //   image.putNull("filename");
      // }
      // if (includeFileSize) {
      //   image.putDouble("fileSize", media.getLong(sizeIndex));
      // } else {
      //   image.putNull("fileSize");
      // }
      
      File file = new File(media.getString(dataIndex));  //changed line
      String strFileName = file.getName();   //changed line
      image.putString("filename", strFileName);    //changed line
      image.putDouble("fileSize", media.getLong(sizeIndex));    //changed line
      
      if (includePlayableDuration || !isVideo) {
        return true;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-04
        • 2021-11-30
        • 2017-09-21
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 2018-08-15
        相关资源
        最近更新 更多