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



我正在尝试让flaft_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中,我已经将fluft_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(...){...}或我代码的任何其他部分,请告诉我。

您没有正确初始化您的flatterSound实例

@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();
}

最新更新