我正在尝试使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();
}