【发布时间】:2021-02-27 03:29:07
【问题描述】:
我正在尝试录制语音并将其上传到 Firebase 存储。 但无法记录记录。 录制语音我使用flutter_sound 插件。 并且无法识别,其中存储了可变语音。实际上我已经从插件示例中复制了这段代码并粘贴到我的项目中。
但是当我按下按钮startRecorder() 录制语音时,计时器文本没有改变意味着录制没有开始。
因为得到异常Audio session is not open
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;
}
}
【问题讨论】:
-
请编辑问题以描述您为调试此代码所做的工作,以及它如何未按您预期的方式工作。
-
在这种情况下,听起来问题与在 Firebase 中存储记录的数据无关。我强烈建议在尽可能少的技术(和尽可能少的代码)中隔离问题,以增加具有正确知识的人能够/将提供帮助的机会。见how to create a minimal, complete, verifiable example。
标签: firebase flutter dart firebase-storage voice-recording