如何录制语音并将其存储到Flatter中的firebase存储?
我正在尝试录制语音并将其上传到firebase存储。 但无法记录。 对于录音,我使用插件。 而且无法识别存储可变语音的位置。实际上,我已经从插件示例中复制了此代码并粘贴到我的项目中 但当我按下按钮录制语音时,计时器文本没有改变,意味着录制没有开始。 因为获取异常如何录制语音并将其存储到Flatter中的firebase存储?,firebase,flutter,dart,firebase-storage,voice-recording,Firebase,Flutter,Dart,Firebase Storage,Voice Recording,我正在尝试录制语音并将其上传到firebase存储。 但无法记录。 对于录音,我使用插件。 而且无法识别存储可变语音的位置。实际上,我已经从插件示例中复制了此代码并粘贴到我的项目中 但当我按下按钮录制语音时,计时器文本没有改变,意味着录制没有开始。 因为获取异常音频会话未打开 String _recorderTxt = '00:00:00'; 录音: void startRecorder() async { try { // Request Microphone perm
音频会话未打开
String _recorderTxt = '00:00:00';
录音:
void startRecorder() async {
try {
// Request Microphone permission if needed
if (!kIsWeb){
var status = (await Permissions.cameraAndMicrophonePermissionsGranted()) ;
// Permission.
// .microphone.request();
if (status==false){
throw RecordingPermissionException("Microphone permission not granted");
}
}
String path = '';
if (!kIsWeb){
Directory tempDir = await getTemporaryDirectory();
path = '${tempDir.path}/flutter_sound${ext[_codec.index]}';
} else{
path = '_flutter_sound${ext[_codec.index]}';
}
await recorderModule.startRecorder
(
toFile: path,
codec: _codec,
bitRate: 8000,
numChannels: 1,
sampleRate: SAMPLE_RATE,
);
print('startRecorder');
_recorderSubscription = recorderModule.onProgress.listen((e) {
if (e != null && e.duration != null) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(
e.duration.inMilliseconds,
isUtc: true);
String txt = DateFormat('mm:ss:SS', 'en_GB').format(date);
this.setState(() {
_recorderTxt = txt.substring(0, 8);
_dbLevel = e.decibels;
});
}
});
this.setState(() {
this._isRecording = true;
this._path[_codec.index] = path;
});
} catch (err) {
print('startRecorder error: $err');
setState(() {
stopRecorder();
this._isRecording = false;
cancelRecordingDataSubscription();
cancelRecorderSubscriptions();
});
}
}
开始语音录制时显示计时器:
Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.only(top: 12.0, bottom: 16.0),
child: Text(
"${this._recorderTxt}",
style: TextStyle(
fontSize: 35.0,
color: Colors.black,
),
),
),
将语音存储到firebase存储:
Future<String> uploadAudioToStorage(File audioFile) async {
try {
StorageReference ref = FirebaseStorage.instance.ref().child('chatAudios/${DateTime.now().millisecondsSinceEpoch}');
StorageUploadTask uploadTask = ref.putFile(audioFile, StorageMetadata(contentType: 'audio/wav'));
// Uri downloadUrl = (await uploadTask.onComplete).uploadSessionUri;
var downloadUrl = (await uploadTask.onComplete).ref.getDownloadURL();
final String url = await downloadUrl;
print("url:$url");
return url;
} catch (error) {
print("error$error");
return null;
}
}
未来上传音频存储(文件音频文件)异步{
试一试{
StorageReference ref=FirebaseStorage.instance.ref().child('chatAudios/${DateTime.now().millissecondssinceepoch}');
StorageUploadTask uploadTask=ref.putFile(音频文件,StorageMetadata(contentType:'audio/wav'));
//Uri downloadUrl=(等待uploadTask.onComplete);
var downloadUrl=(wait uploadTask.onComplete).ref.getDownloadURL();
最终字符串url=等待下载url;
打印(“url:$url”);
返回url;
}捕获(错误){
打印(“错误$error”);
返回null;
}
}
请编辑此问题,描述您调试此代码所做的工作,以及它的工作方式与您的预期不符。在这种情况下,问题似乎与在Firebase中存储记录的数据无关。我强烈建议用尽可能少的技术(和尽可能少的代码)来隔离问题,以增加有正确知识的人能够/将会提供帮助的机会。看见