Flutter - 记录声音并上传到 Firebase

4

我正在使用Flutter Sound记录音频,并且使用Firebase Storage上传。

这2个功能用于管理录制:

  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
@Gauranga 不是。 - Cracin
1个回答

0

试试这种方式 初始化音频录制器插件的一个实例...

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会自动获取当前用户数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接