From 2cf4f47950329bafffb2ebf70a4a7137d41be9f5 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 30 Apr 2021 17:09:26 +0200 Subject: [PATCH] refactor: Switch to record package --- lib/views/recording_dialog.dart | 39 ++++++++++++++------------------- pubspec.lock | 11 ++++++++-- pubspec.yaml | 1 + 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/views/recording_dialog.dart b/lib/views/recording_dialog.dart index 1388ecff..57f11362 100644 --- a/lib/views/recording_dialog.dart +++ b/lib/views/recording_dialog.dart @@ -4,10 +4,11 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:flutter_sound_lite/flutter_sound.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:record/record.dart'; class RecordingDialog extends StatefulWidget { + static const String recordingFileType = 'mp3'; const RecordingDialog({ Key key, }) : super(key: key); @@ -17,23 +18,19 @@ class RecordingDialog extends StatefulWidget { } class _RecordingDialogState extends State { - final FlutterSoundRecorder flutterSound = FlutterSoundRecorder(); String time = '00:00:00'; - StreamSubscription _recorderSubscription; + Timer _recorderSubscription; + Duration _duration; bool error = false; String _recordedPath; - double _decibels = 0; void startRecording() async { try { - await flutterSound.openAudioSession(); - await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100)); - - final codec = Codec.aacADTS; final tempDir = await getTemporaryDirectory(); - _recordedPath = '${tempDir.path}/recording${ext[codec.index]}'; + _recordedPath = + '${tempDir.path}/recording_${DateTime.now().millisecondsSinceEpoch}.${RecordingDialog.recordingFileType}'; // delete any existing file final outputFile = File(_recordedPath); @@ -41,15 +38,11 @@ class _RecordingDialogState extends State { await outputFile.delete(); } - await flutterSound.startRecorder(codec: codec, toFile: _recordedPath); - - _recorderSubscription = flutterSound.onProgress.listen((e) { - setState(() { - _decibels = e.decibels; - time = - '${e.duration.inMinutes.toString().padLeft(2, '0')}:${(e.duration.inSeconds % 60).toString().padLeft(2, '0')}'; - }); - }); + await Record.start(path: _recordedPath); + setState(() => _duration = Duration.zero); + _recorderSubscription?.cancel(); + _recorderSubscription = Timer.periodic(Duration(seconds: 1), + (_) => setState(() => _duration += Duration(seconds: 1))); } catch (e) { error = true; } @@ -63,9 +56,8 @@ class _RecordingDialogState extends State { @override void dispose() { - if (flutterSound.isRecording) flutterSound.stopRecorder(); _recorderSubscription?.cancel(); - flutterSound.closeAudioSession(); + Record.stop(); super.dispose(); } @@ -77,7 +69,8 @@ class _RecordingDialogState extends State { }); } const maxDecibalWidth = 64.0; - final decibalWidth = min(_decibels / 2, maxDecibalWidth).toDouble(); + final decibalWidth = + min(_duration.inSeconds % 2 / 2, maxDecibalWidth).toDouble(); return AlertDialog( content: Row( children: [ @@ -118,8 +111,8 @@ class _RecordingDialogState extends State { ), TextButton( onPressed: () async { - await _recorderSubscription?.cancel(); - await flutterSound.stopRecorder(); + _recorderSubscription?.cancel(); + await Record.stop(); Navigator.of(context, rootNavigator: false) .pop(_recordedPath); }, diff --git a/pubspec.lock b/pubspec.lock index 174c5bdb..0e74f4f8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -881,6 +881,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.4.5" + record: + dependency: "direct main" + description: + name: record + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" rxdart: dependency: transitive description: @@ -1251,5 +1258,5 @@ packages: source: hosted version: "3.1.0" sdks: - dart: ">=2.12.0 <3.0.0" - flutter: ">=2.0.1" + dart: ">=2.12.1 <3.0.0" + flutter: ">=2.0.2" diff --git a/pubspec.yaml b/pubspec.yaml index 66f319af..03ff22b8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -59,6 +59,7 @@ dependencies: permission_handler: ^6.1.3 provider: ^5.0.0 receive_sharing_intent: ^1.4.5 + record: ^2.1.1 scroll_to_index: ^2.0.0 sentry: ^5.0.0 share: ^2.0.1