cameraController.stopVideoRecording() 返回带有 .temp 扩展名的 XFile,该文件应该是 mp4 并保存到图库

问题描述 投票:0回答:1

这是录像机应用程序,因此应该立即开始录像,这就是我调用

startCamera
方法的原因。 当视频录制完成时,客户端单击停止录制按钮,然后
stopRecording
方法触发, 之后我得到
XFile
.

我的问题是

当我打印路径时它会显示

I/flutter (11116): Inside Recording stopped /data/user/0/com.example.design4you/cache/REC7110041240717347649.temp

预期的扩展名是 mp4 但我得到了 temp, 我还想通过 API 发送该视频并在设备(图库)上保存副本。

这是我的完整代码:-

 void initState() {
    print('Inside initState');
    startCamera();
    super.initState();
  }



 void startCamera() async {
    print('Inside startCamera');
    cameraController = CameraController(cameras[1], ResolutionPreset.medium);
    cameraValue = cameraController.initialize();
    await Future.delayed(Duration(seconds: 2));
    startRecording();
  }



 void startRecording() async {
    print('Inside startRecording');

    try {
      print('Inside startRecording Try block');

      //Starts recording video
      await cameraValue;
      await cameraController.prepareForVideoRecording();

      await cameraController.startVideoRecording();
    } catch (e) {
      print("inside catch error message" + e.toString());
      Get.snackbar('title', e.toString());
    }

   
  }

  void stopRecording() async {
    print('Inside stop recording');
    XFile video = await cameraController.stopVideoRecording();

    print('Inside Recording stopped ' + video.path);

    Get.snackbar('video.path', video.path);

    try {
      await Gal.putVideo(video.path);
    } catch (e) {
      print("inside Catch" + e.toString());
    }
  }

这是调试控制台的输出

I/flutter (11116): Inside initState
I/flutter (11116): Inside startCamera
I/flutter (11116): Inside startRecording
I/flutter (11116): Inside startRecording Try block
I/flutter (11116): Inside stop recording
I/flutter (11116): Inside Recording stopped /data/user/0/com.example.design4you/cache/REC7110041240717347649.temp
I/flutter (11116): inside [GalException/UNEXPECTED]: An unexpected error has occurred.

帮助是有价的。

flutter
1个回答
0
投票

您需要使用 .mp4 扩展名重新命名您的文件。 完整工作代码

void stopRecording() async {
    print('Inside stop recording');
    XFile video = await cameraController.stopVideoRecording();

    final Directory tempDir = await getTemporaryDirectory();
    final String tempPath = video.path;
    final String newFileName =
        path.join(tempDir.path, '${DateTime.now().millisecondsSinceEpoch}.mp4');
    final File tempFile = File(tempPath);
    final File newFile = tempFile.renameSync(newFileName);

    print('Inside Recording stopped ' + video.path);

    try {
      await Gal.putVideo(newFile.path);
      print('Inside Try');
    } catch (e) {
      print("inside Catch" + e.toString());
    }
  }

如果您仍然遇到任何错误,请使用调试控制台输出进行回复。

© www.soinside.com 2019 - 2024. All rights reserved.