繁体   English   中英

无法从 flutter_sound 包中成功调用 startRecorder(uri: ...)

[英]Unable to successfully call startRecorder(uri: ...) from the flutter_sound package

我试图让 flutter_sound 包的简单功能发挥作用。 我通过首先能够开始录制来解决这个问题。 似乎我目前有一个问题,即使我将 Uri 包含到我想保存这些文件的位置,以及我希望它导出这些文件的音频格式类型,因为我遇到了一个问题:

flutter: NoSuchMethodError: The method 'startRecorder' was called on null.
Receiver: null
Tried calling: startRecorder(codec: Instance of 't_CODEC', uri: "/Users/<user-name>/Library/Developer/CoreSimulator/Devices/CEC7EE26-9400-479B-85EB-06728074F7C0/data/Containers/Data/Application/4C726628-AE3B-42E7-8C6A-4B239AFDFAE6/Library/Caches/audio.aac")

我检查了目录,没有看到audio.aac文件出现,所以当我尝试通过终端使用touch audio.aac创建文件并尝试录制时,我仍然遇到同样的错误。 所以我假设我需要能够以某种方式创建该文件,或者我可能根本不明白问题是什么。 我真的很迷茫,有其他开发人员尝试提供帮助,但找不到有关如何解决此问题的资源。

在我的 pubspec.yaml 中,我将 flutter_sound 版本设置为如下所示:

dev_dependencies:
  flutter_sound: ^2.0.3

我已经在我的 Info.plist 和我的 AndroidManifest.xml 中包含了必要的权限。

我将发布,我觉得下面需要代码。

class _AudioRecorderState extends State<AudioRecorder> {
  String _recordedFilePath;
  bool _isRecording = false;
  bool _isPlaying = false;
  StreamSubscription _recorderSubscription;
  StreamSubscription _dbPeakSubscription;
  StreamSubscription _playerSubscription;
  FlutterSound flutterSound;

  String _recorderTxt = '00:00:00';
  String _playerTxt = '00:00:00';
  double _dbLevel;

  double sliderCurrentPosition = 0.0;
  double maxDuration = 1.0;

  @override
  void initState() {
    super.initState();
    FlutterSound flutterSound = new FlutterSound();
    flutterSound.setSubscriptionDuration(0.01);
    flutterSound.setDbPeakLevelUpdate(0.8);
    flutterSound.setDbLevelEnabled(true);
    initializeDateFormatting();
  }

  void startRecorder() async {
    try {
      Directory tempDir = await getTemporaryDirectory();
      File filePath = File('${tempDir.path}/audio.aac');
      String path = await flutterSound.startRecorder(uri: filePath.path, codec: t_CODEC.CODEC_AAC);
      print('startRecorder: $path');

      _recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
        DateTime date = new DateTime.fromMillisecondsSinceEpoch(
            e.currentPosition.toInt(),
            isUtc: true);
        String txt = DateFormat('mm:ss:SS', 'en_US').format(date);

        this.setState(() {
          this._recorderTxt = txt.substring(0, 8);
        });
      });
      _dbPeakSubscription =
          flutterSound.onRecorderDbPeakChanged.listen((value) {
        print("got update -> $value");
        setState(() {
          this._dbLevel = value;
        });
      });

      this.setState(() {
        this._isRecording = true;
      });
    } catch (err) {
      print('startRecorder error: $err');
    }
  }

如果您想查看我的Widget Build(...){...}或我代码的任何其他部分,请告诉我。

你没有正确初始化你的 flutterSound 实例

 @override
  void initState() {
    super.initState();
    //FlutterSound flutterSound = new FlutterSound();
    //you already created flutterSound at the top
    flutterSound = new FlutterSound();
    flutterSound.setSubscriptionDuration(0.01);
    flutterSound.setDbPeakLevelUpdate(0.8);
    flutterSound.setDbLevelEnabled(true);
    initializeDateFormatting();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM