Flutter - 录制语音并将其上传到 firebase
Posted
技术标签:
【中文标题】Flutter - 录制语音并将其上传到 firebase【英文标题】:Flutter - Record voice and upload it to firebase 【发布时间】:2020-04-12 08:24:17 【问题描述】:我使用flutter_sound
录制音频并使用firebase_storage
上传。
录制由以下两个功能管理:
void startRecording() async
result = await flutterSound.startRecorder(null);
aname = result;
print('startRecorder: $result');
_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e)
if(e != null)
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
);
void stopRecording() async
if(flutterSound.isRecording) result = await flutterSound.stopRecorder();
打印语句打印以下内容:
I/flutter (11507): startRecorder: /data/user/0/com.example.appx/app_flutter/null
I/flutter (11507): stopRecorder: /data/user/0/com.example.appx/app_flutter/null
上传应该通过这个代码来完成:
var uuid = Uuid();
String aname = uuid.v4();
final StorageReference storageRef = FirebaseStorage.instance.ref().child('recordings');
final StorageUploadTask uploadTask = storageRef.putFile(
File(aname),
StorageMetadata(
contentType: 'audio/aac',
),
);
final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
final String url = (await downloadUrl.ref.getDownloadURL());
但是当我尝试获取 URL 时出现以下错误:
Running Gradle task 'assembleProfile'...
E/flutter (11507): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(download_error, The operation retry limit has been exceeded., null)
E/flutter (11507): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569)
E/flutter (11507): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321)
E/flutter (11507): <asynchronous suspension>
E/flutter (11507): #2 StorageReference.getDownloadURL (package:firebase_storage/src/storage_reference.dart:142)
E/flutter (11507): #3 updateUserAlert (package:electroshoex/utils.dart:58)
E
任何帮助将不胜感激。谢谢你。 :)
【问题讨论】:
你解决了吗? @Gauranga Nope. 【参考方案1】:试试这个方法 初始化录音机插件的实例...
Recording _recording;
StorageReference storageReference =
FirebaseStorage.instance.ref().child('Recordings');
final StorageUploadTask uploadTask = storageReference.putFile(
File(_recording.path), StorageMetadata(contentType: 'audio/wav'));
将 _recording.path 作为字符串添加到 firebase 的 putFile 方法中,并记得最后向 StorageMetaData 添加内容类型属性,建议在物理设备上运行它,这样 firebase 会自动获取当前用户数据.
【讨论】:
以上是关于Flutter - 录制语音并将其上传到 firebase的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Javascript / PHP录制用户的声音? [关闭]