fluffychat/lib/pages/recording_dialog.dart

177 lines
5.4 KiB
Dart
Raw Normal View History

2020-03-15 10:27:51 +00:00
import 'dart:async';
2021-08-12 07:48:10 +00:00
import 'package:flutter/cupertino.dart';
2020-03-15 10:27:51 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:path_provider/path_provider.dart';
2021-04-30 15:09:26 +00:00
import 'package:record/record.dart';
import 'package:wakelock/wakelock.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/sentry_controller.dart';
2020-03-15 10:27:51 +00:00
class RecordingDialog extends StatefulWidget {
static const String recordingFileType = 'm4a';
2021-01-19 14:46:43 +00:00
const RecordingDialog({
Key key,
}) : super(key: key);
2020-03-15 10:27:51 +00:00
@override
_RecordingDialogState createState() => _RecordingDialogState();
}
class _RecordingDialogState extends State<RecordingDialog> {
2021-04-30 15:09:26 +00:00
Timer _recorderSubscription;
2021-05-01 13:49:29 +00:00
Duration _duration = Duration.zero;
2020-03-15 10:27:51 +00:00
2020-03-15 10:49:59 +00:00
bool error = false;
String _recordedPath;
2021-07-08 15:20:38 +00:00
final _audioRecorder = Record();
2021-08-11 19:21:08 +00:00
Amplitude _amplitude;
2020-03-15 10:49:59 +00:00
2021-10-16 07:59:38 +00:00
Future<void> startRecording() async {
2020-03-15 10:49:59 +00:00
try {
final tempDir = await getTemporaryDirectory();
2021-04-30 15:09:26 +00:00
_recordedPath =
2021-05-07 17:54:32 +00:00
'${tempDir.path}/recording${DateTime.now().microsecondsSinceEpoch}.${RecordingDialog.recordingFileType}';
2021-07-08 15:20:38 +00:00
final result = await _audioRecorder.hasPermission();
2021-05-01 19:36:27 +00:00
if (result != true) {
setState(() => error = true);
return;
}
await Wakelock.enable();
2021-07-08 15:20:38 +00:00
await _audioRecorder.start(
path: _recordedPath, encoder: AudioEncoder.AAC);
2021-04-30 15:09:26 +00:00
setState(() => _duration = Duration.zero);
_recorderSubscription?.cancel();
2021-08-11 20:04:55 +00:00
_recorderSubscription =
2021-10-14 16:09:30 +00:00
Timer.periodic(const Duration(milliseconds: 100), (_) async {
2021-08-11 19:21:08 +00:00
_amplitude = await _audioRecorder.getAmplitude();
setState(() {
2021-10-14 16:09:30 +00:00
_duration += const Duration(milliseconds: 100);
2021-08-11 19:21:08 +00:00
});
});
2021-05-01 19:36:27 +00:00
} catch (e, s) {
SentryController.captureException(e, s);
setState(() => error = true);
2020-03-15 10:49:59 +00:00
}
2020-03-15 10:27:51 +00:00
}
@override
void initState() {
super.initState();
startRecording();
}
@override
void dispose() {
Wakelock.disable();
2020-03-15 10:27:51 +00:00
_recorderSubscription?.cancel();
2021-07-08 15:20:38 +00:00
_audioRecorder.stop();
2020-03-15 10:27:51 +00:00
super.dispose();
}
@override
Widget build(BuildContext context) {
const maxDecibalWidth = 64.0;
2021-08-11 20:04:55 +00:00
final decibalWidth =
2021-08-12 07:44:19 +00:00
((_amplitude == null || _amplitude.current == double.negativeInfinity
? 0
: _amplitude.current / _amplitude.max)
.abs() +
1) *
2021-08-11 20:04:55 +00:00
(maxDecibalWidth / 4).toDouble();
2021-05-01 13:49:29 +00:00
final time =
'${_duration.inMinutes.toString().padLeft(2, '0')}:${(_duration.inSeconds % 60).toString().padLeft(2, '0')}';
2021-08-12 07:48:10 +00:00
final content = error
? Text(L10n.of(context).oopsSomethingWentWrong)
: Row(
children: <Widget>[
Container(
width: maxDecibalWidth,
height: maxDecibalWidth,
alignment: Alignment.center,
child: AnimatedContainer(
2021-10-14 16:09:30 +00:00
duration: const Duration(milliseconds: 100),
2021-08-12 07:48:10 +00:00
width: decibalWidth,
height: decibalWidth,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(decibalWidth),
2021-05-01 19:36:27 +00:00
),
),
2021-08-12 07:48:10 +00:00
),
2021-10-14 16:09:30 +00:00
const SizedBox(width: 8),
2021-08-12 07:48:10 +00:00
Expanded(
child: Text(
'${L10n.of(context).recording}: $time',
2021-10-14 16:09:30 +00:00
style: const TextStyle(
2021-08-12 07:48:10 +00:00
fontSize: 18,
2021-05-01 19:36:27 +00:00
),
),
2021-08-12 07:48:10 +00:00
),
],
);
if (PlatformInfos.isCupertinoStyle) {
return CupertinoAlertDialog(
content: content,
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.of(context, rootNavigator: false).pop(),
child: Text(
L10n.of(context).cancel.toUpperCase(),
style: TextStyle(
color:
Theme.of(context).textTheme.bodyText2.color.withAlpha(150),
),
2020-03-15 10:27:51 +00:00
),
2021-08-12 07:48:10 +00:00
),
if (error != true)
CupertinoDialogAction(
onPressed: () async {
_recorderSubscription?.cancel();
await _audioRecorder.stop();
Navigator.of(context, rootNavigator: false)
.pop<String>(_recordedPath);
},
child: Text(L10n.of(context).send.toUpperCase()),
),
],
);
}
return AlertDialog(
content: content,
actions: [
2021-02-27 06:53:34 +00:00
TextButton(
2021-03-04 11:28:06 +00:00
onPressed: () => Navigator.of(context, rootNavigator: false).pop(),
2020-03-15 10:27:51 +00:00
child: Text(
2021-02-24 11:17:23 +00:00
L10n.of(context).cancel.toUpperCase(),
2020-03-15 10:27:51 +00:00
style: TextStyle(
2020-05-06 16:43:30 +00:00
color: Theme.of(context).textTheme.bodyText2.color.withAlpha(150),
2020-03-15 10:27:51 +00:00
),
),
),
2021-05-01 19:36:27 +00:00
if (error != true)
TextButton(
onPressed: () async {
_recorderSubscription?.cancel();
2021-07-08 15:20:38 +00:00
await _audioRecorder.stop();
2021-05-01 19:36:27 +00:00
Navigator.of(context, rootNavigator: false)
.pop<String>(_recordedPath);
},
child: Row(
2021-05-31 17:13:57 +00:00
mainAxisSize: MainAxisSize.min,
2021-05-01 19:36:27 +00:00
children: <Widget>[
Text(L10n.of(context).send.toUpperCase()),
2021-10-14 16:09:30 +00:00
const SizedBox(width: 4),
const Icon(Icons.send_outlined, size: 15),
2021-05-01 19:36:27 +00:00
],
),
2020-03-15 10:27:51 +00:00
),
],
);
}
}