flutter 文件合成

  Future<String> _combineRecordedFiles() async {
    String savePath = '';
    String savePathPcm = '';
    String saveDirPath = await Utils.getSaveDirPath(Cons.BLOCK_TYPE_VOICE);
    if (GetPlatform.isWindows) {
      savePath = "$saveDirPath\\${randomId()}.wav";
    } else if (GetPlatform.isMacOS) {
      savePath = "$saveDirPath/${randomId()}.wav";
    }
    try {
      await File(savePath).create(recursive: true);
      // 初始化一个空的Uint8List来存储所有文件的字节数据
      Uint8List combinedBytes = Uint8List(0);
      LogUtils.e('savePath =====> : $savePath');

      // var outputFile = File(savePath).openWrite();
      await Future.forEach(_recordedFiles, (String filePath) async {
        File file = File(filePath);
        if (file.existsSync()) {
          // await outputFile.addStream(file.openRead());
          await convertWavToPcm(filePath, savePathPcm);
          List<int> bytes = await File(savePathPcm).readAsBytes();
          combinedBytes = Uint8List.fromList([...combinedBytes, ...bytes]);
        }
      });
      // Close the output file
      // await outputFile.close();
      // 将累积的所有字节写入到输出文件
      // await outputFile.writeFrom(bytes: combinedBytes);
      await File(savePath).writeAsBytes(combinedBytes);
      // await outputFile.flush(); // 确保数据被立即写入磁盘
      // await outputFile.close();
      LogUtils.i('Combined audio saved at: $savePath');
    } catch (e) {
      LogUtils.e('Error concatenating files: $e');
    }
    return savePath;
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容