refactor: Switch to record package

This commit is contained in:
Christian Pauly 2021-04-30 17:09:26 +02:00
parent aa3348e72e
commit 2cf4f47950
3 changed files with 26 additions and 25 deletions

View File

@ -4,10 +4,11 @@ import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.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:path_provider/path_provider.dart';
import 'package:record/record.dart';
class RecordingDialog extends StatefulWidget { class RecordingDialog extends StatefulWidget {
static const String recordingFileType = 'mp3';
const RecordingDialog({ const RecordingDialog({
Key key, Key key,
}) : super(key: key); }) : super(key: key);
@ -17,23 +18,19 @@ class RecordingDialog extends StatefulWidget {
} }
class _RecordingDialogState extends State<RecordingDialog> { class _RecordingDialogState extends State<RecordingDialog> {
final FlutterSoundRecorder flutterSound = FlutterSoundRecorder();
String time = '00:00:00'; String time = '00:00:00';
StreamSubscription _recorderSubscription; Timer _recorderSubscription;
Duration _duration;
bool error = false; bool error = false;
String _recordedPath; String _recordedPath;
double _decibels = 0;
void startRecording() async { void startRecording() async {
try { try {
await flutterSound.openAudioSession();
await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100));
final codec = Codec.aacADTS;
final tempDir = await getTemporaryDirectory(); final tempDir = await getTemporaryDirectory();
_recordedPath = '${tempDir.path}/recording${ext[codec.index]}'; _recordedPath =
'${tempDir.path}/recording_${DateTime.now().millisecondsSinceEpoch}.${RecordingDialog.recordingFileType}';
// delete any existing file // delete any existing file
final outputFile = File(_recordedPath); final outputFile = File(_recordedPath);
@ -41,15 +38,11 @@ class _RecordingDialogState extends State<RecordingDialog> {
await outputFile.delete(); await outputFile.delete();
} }
await flutterSound.startRecorder(codec: codec, toFile: _recordedPath); await Record.start(path: _recordedPath);
setState(() => _duration = Duration.zero);
_recorderSubscription = flutterSound.onProgress.listen((e) { _recorderSubscription?.cancel();
setState(() { _recorderSubscription = Timer.periodic(Duration(seconds: 1),
_decibels = e.decibels; (_) => setState(() => _duration += Duration(seconds: 1)));
time =
'${e.duration.inMinutes.toString().padLeft(2, '0')}:${(e.duration.inSeconds % 60).toString().padLeft(2, '0')}';
});
});
} catch (e) { } catch (e) {
error = true; error = true;
} }
@ -63,9 +56,8 @@ class _RecordingDialogState extends State<RecordingDialog> {
@override @override
void dispose() { void dispose() {
if (flutterSound.isRecording) flutterSound.stopRecorder();
_recorderSubscription?.cancel(); _recorderSubscription?.cancel();
flutterSound.closeAudioSession(); Record.stop();
super.dispose(); super.dispose();
} }
@ -77,7 +69,8 @@ class _RecordingDialogState extends State<RecordingDialog> {
}); });
} }
const maxDecibalWidth = 64.0; const maxDecibalWidth = 64.0;
final decibalWidth = min(_decibels / 2, maxDecibalWidth).toDouble(); final decibalWidth =
min(_duration.inSeconds % 2 / 2, maxDecibalWidth).toDouble();
return AlertDialog( return AlertDialog(
content: Row( content: Row(
children: <Widget>[ children: <Widget>[
@ -118,8 +111,8 @@ class _RecordingDialogState extends State<RecordingDialog> {
), ),
TextButton( TextButton(
onPressed: () async { onPressed: () async {
await _recorderSubscription?.cancel(); _recorderSubscription?.cancel();
await flutterSound.stopRecorder(); await Record.stop();
Navigator.of(context, rootNavigator: false) Navigator.of(context, rootNavigator: false)
.pop<String>(_recordedPath); .pop<String>(_recordedPath);
}, },

View File

@ -881,6 +881,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.4.5" version: "1.4.5"
record:
dependency: "direct main"
description:
name: record
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
rxdart: rxdart:
dependency: transitive dependency: transitive
description: description:
@ -1251,5 +1258,5 @@ packages:
source: hosted source: hosted
version: "3.1.0" version: "3.1.0"
sdks: sdks:
dart: ">=2.12.0 <3.0.0" dart: ">=2.12.1 <3.0.0"
flutter: ">=2.0.1" flutter: ">=2.0.2"

View File

@ -59,6 +59,7 @@ dependencies:
permission_handler: ^6.1.3 permission_handler: ^6.1.3
provider: ^5.0.0 provider: ^5.0.0
receive_sharing_intent: ^1.4.5 receive_sharing_intent: ^1.4.5
record: ^2.1.1
scroll_to_index: ^2.0.0 scroll_to_index: ^2.0.0
sentry: ^5.0.0 sentry: ^5.0.0
share: ^2.0.1 share: ^2.0.1