有没有办法将flutter_tts文件保存到firebase存储?
Posted
技术标签:
【中文标题】有没有办法将flutter_tts文件保存到firebase存储?【英文标题】:Is there any way to save flutter_tts file to firebase storage? 【发布时间】:2021-12-18 10:14:04 【问题描述】:我正在开发一个颤振项目,其中用户应该创建一些脚本并通过在文本中输入它们,然后 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;
【问题讨论】:
【参考方案1】:花了一整天的时间,在朋友的帮助下,我终于弄清楚了是因为我同时使用synthesizeToFile()
和speak()
函数而导致的问题,我设法解决了通过将我的代码更改为以下代码 sn-p 来发出问题。
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;
【讨论】:
以上是关于有没有办法将flutter_tts文件保存到firebase存储?的主要内容,如果未能解决你的问题,请参考以下文章
Quartus设计FIR滤波器的系数文件格式(适用于FIR II的IP核)
有没有办法使用 ffmpeg 将 GIF 的所有帧保存到 PNG 中?
有没有办法自动将命令历史保存到 cmd.exe 中的文件中,类似于 bash 的 bash_history?