Flutter-录制语音并将其上传到Firebase

时间:2019-12-19 18:01:53

标签: firebase flutter firebase-storage

我正在使用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

任何帮助将不胜感激。谢谢。 :)

1 个答案:

答案 0 :(得分: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方法中,并记住最后向ContentMetaData添加内容类型属性,建议在物理设备上运行它,因此firebase会自动获取当前用户数据