dart 有没有办法保存flutter_tts文件到firebase存储?

nc1teljy  于 2023-11-14  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我在一个Flutter项目中,用户应该创建一些脚本,并通过键入文本,然后flutter_tts库应该将它们转换为音频文件,这是目前的工作正常,但我想保存该文件到firebase存储为以后的用户工作。我已经尝试了以下代码,但它只是保存在firebase存储空白音频文件。任何形式的帮助将不胜感激。
我尝试的代码是:

final FlutterTts _flutterTts = FlutterTts();
late var fileName;
/// creation of audio script
Future createAudioScript(
  String name,
  String script,
  String firebasepath,
) async {
  await _flutterTts.setLanguage("en-US");
  await _flutterTts.setSpeechRate(1.0);
  await _flutterTts.setVolume(1.0);
  await _flutterTts.setPitch(1.0);
  await _flutterTts.setVoice(
    {"name": "en-us-x-tpf-local", "locale": "en-US"},
  );
  await _flutterTts.speak(script);
  fileName = GetPlatform.isAndroid ? '$name.wav' : '$name.caf';
  print('FileName: $fileName');

  var directoryPath =
      "${(await getApplicationDocumentsDirectory()).path}/audio/";
  var directory = Directory(directoryPath);
  if (!await directory.exists()) {
    await directory.create();
    print('[INFO] Created the directory');
  }

  var path =
      "${(await getApplicationDocumentsDirectory()).path}/audio/$fileName";
  print('[INFO] path: $path');
  var file = File(path);
  if (!await file.exists()) {
    await file.create();
    print('[INFO] Created the file');
  }

  await _flutterTts.synthesizeToFile(script, fileName).then((value) async {
    if (value == 1) {
      print('generated');
      var file = File(
        '/storage/emulated/0/Android/data/com.solution.thriving/files/$fileName',
      );
      print(file);
      moveFile(file, path, '$firebasepath/$fileName').then((value) {
        print('move file: $value');
        _app.link.value = value;
        print('link: ${_app.link.value}');
      });
    }
  });
}

/// move file from temporary to local storage and save to firebase
Future<String> moveFile(
  File sourceFile,
  String newPath,
  String firebasePath,
) async {
  String audioLink = '';
  print('moved');
  await sourceFile.copy(newPath).then((value) async {
    print('value: $value');
    await appStorage.uploadAudio(value, fileName, firebasePath).then((audio) {
      print(audio);
      audioLink = audio;
      return audioLink;
    });
  }).whenComplete(() async {
    customToast(message: 'Audio has been generated successfully.');
  });
  return audioLink;
}

字符串

sgtfey8w

sgtfey8w1#

在一个朋友的帮助下,我花了一整天的时间,终于找到了问题所在,因为我同时使用了synthesizeToFile()speak()函数,我通过将代码改为下面的代码片段来解决这个问题。

final FlutterTts _flutterTts = FlutterTts();
late var fileName;
/// converting text to speech
Future createAudioScript(
  String name,
  String script,
  String firebasepath,
) async {
  await _flutterTts.setLanguage("en-US");
  await _flutterTts.setSpeechRate(1.0);
  await _flutterTts.setVolume(1.0);
  await _flutterTts.setPitch(1.0);
  await _flutterTts.setVoice(
    {"name": "en-us-x-tpf-local", "locale": "en-US"},
  );
  if (GetPlatform.isIOS) _flutterTts.setSharedInstance(true);
  // await _flutterTts.speak(script);
  fileName = GetPlatform.isAndroid ? '$name.wav' : '$name.caf';
  log('FileName: $fileName');

  await _flutterTts.synthesizeToFile(script, fileName).then((value) async {
    if (value == 1) {
      log('Value $value');
      log('generated');
    }
  });
  final externalDirectory = await getExternalStorageDirectory();
  var path = '${externalDirectory!.path}/$fileName';
  log(path);
  saveToFirebase(path, fileName, firebasPath: '$firebasepath/$name')
      .then((value) => {log('Received Audio Link: $value')});
}
/// saving converted audio file to firebase
Future<String> saveToFirebase(String path, String name,
    {required String firebasPath}) async {
  final firebaseStorage = FirebaseStorage.instance;
  SettableMetadata metadata = SettableMetadata(
    contentType: 'audio/mpeg',
    customMetadata: <String, String>{
      'userid': _app.userid.value,
      'name': _app.name.value,
      'filename': name,
    },
  );
  var snapshot = await firebaseStorage
      .ref()
      .child(firebasPath)
      .putFile(File(path), metadata);
  var downloadUrl = await snapshot.ref.getDownloadURL();
  print(downloadUrl + " saved url");
  return downloadUrl;
}

字符串

相关问题