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