fluffychat/lib/pages/chat/recording_dialog.dart

240 lines
7 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_plus/wakelock_plus.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/config/app_config.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
2022-01-01 21:08:19 +00:00
import 'events/audio_player.dart';
2020-03-15 10:27:51 +00:00
class RecordingDialog extends StatefulWidget {
2023-10-30 15:32:21 +00:00
static const String recordingFileType = 'm4a';
2021-01-19 14:46:43 +00:00
const RecordingDialog({
super.key,
});
2020-03-15 10:27:51 +00:00
@override
2022-08-14 14:59:21 +00:00
RecordingDialogState createState() => RecordingDialogState();
2020-03-15 10:27:51 +00:00
}
2022-08-14 14:59:21 +00:00
class RecordingDialogState extends State<RecordingDialog> {
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;
2024-04-07 12:37:22 +00:00
final _audioRecorder = AudioRecorder();
final List<double> amplitudeTimeline = [];
2020-03-15 10:49:59 +00:00
2023-10-30 15:32:21 +00:00
static const int bitRate = 64000;
2024-04-07 12:37:22 +00:00
static const int samplingRate = 44100;
2023-10-30 15:32:21 +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();
2024-04-07 12:37:22 +00:00
final path = _recordedPath =
2023-10-30 15:32:21 +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 WakelockPlus.enable();
2021-07-08 15:20:38 +00:00
await _audioRecorder.start(
2024-04-07 12:37:22 +00:00
const RecordConfig(
bitRate: bitRate,
sampleRate: samplingRate,
numChannels: 1,
autoGain: true,
echoCancel: true,
noiseSuppress: true,
),
path: path,
2021-12-30 11:43:42 +00:00
);
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 {
final amplitude = await _audioRecorder.getAmplitude();
var value = 100 + amplitude.current * 2;
value = value < 1 ? 1 : value;
amplitudeTimeline.add(value);
2021-08-11 19:21:08 +00:00
setState(() {
2021-10-14 16:09:30 +00:00
_duration += const Duration(milliseconds: 100);
2021-08-11 19:21:08 +00:00
});
});
} catch (_) {
2021-05-01 19:36:27 +00:00
setState(() => error = true);
rethrow;
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() {
WakelockPlus.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();
}
2021-10-30 08:39:00 +00:00
void _stopAndSend() async {
_recorderSubscription?.cancel();
await _audioRecorder.stop();
final path = _recordedPath;
if (path == null) throw ('Recording failed!');
2022-01-01 21:08:19 +00:00
const waveCount = AudioPlayerWidget.wavesCount;
final step = amplitudeTimeline.length < waveCount
? 1
2022-01-01 21:08:19 +00:00
: (amplitudeTimeline.length / waveCount).round();
final waveform = <int>[];
for (var i = 0; i < amplitudeTimeline.length; i += step) {
2022-01-02 09:11:55 +00:00
waveform.add((amplitudeTimeline[i] / 100 * 1024).round());
}
Navigator.of(context, rootNavigator: false).pop<RecordingResult>(
RecordingResult(
path: path,
duration: _duration.inMilliseconds,
waveform: waveform,
),
);
2021-10-30 08:39:00 +00:00
}
2020-03-15 10:27:51 +00:00
@override
Widget build(BuildContext context) {
const maxDecibalWidth = 64.0;
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)
2021-08-12 07:48:10 +00:00
: Row(
children: [
2021-08-12 07:48:10 +00:00
Container(
width: 16,
height: 16,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(32),
color: Colors.red,
2021-05-01 19:36:27 +00:00
),
2021-08-12 07:48:10 +00:00
),
Expanded(
2022-01-01 15:23:17 +00:00
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: amplitudeTimeline.reversed
.take(26)
.toList()
.reversed
.map(
(amplitude) => Container(
2022-01-01 15:23:17 +00:00
margin: const EdgeInsets.only(left: 2),
width: 4,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius:
BorderRadius.circular(AppConfig.borderRadius),
),
height: maxDecibalWidth * (amplitude / 100),
),
)
2022-01-01 15:23:17 +00:00
.toList(),
2021-05-01 19:36:27 +00:00
),
2021-08-12 07:48:10 +00:00
),
2022-01-01 15:23:17 +00:00
const SizedBox(width: 8),
SizedBox(
width: 48,
child: Text(time),
),
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(),
2021-08-12 07:48:10 +00:00
style: TextStyle(
color: Theme.of(context)
.textTheme
2023-01-26 08:47:30 +00:00
.bodyMedium
?.color
?.withAlpha(150),
2021-08-12 07:48:10 +00:00
),
2020-03-15 10:27:51 +00:00
),
2021-08-12 07:48:10 +00:00
),
if (error != true)
CupertinoDialogAction(
2021-10-30 08:39:00 +00:00
onPressed: _stopAndSend,
child: Text(L10n.of(context)!.send.toUpperCase()),
2021-08-12 07:48:10 +00:00
),
],
);
}
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(
L10n.of(context)!.cancel.toUpperCase(),
2020-03-15 10:27:51 +00:00
style: TextStyle(
color:
2023-01-26 08:47:30 +00:00
Theme.of(context).textTheme.bodyMedium?.color?.withAlpha(150),
2020-03-15 10:27:51 +00:00
),
),
),
2021-05-01 19:36:27 +00:00
if (error != true)
TextButton(
2021-10-30 08:39:00 +00:00
onPressed: _stopAndSend,
2021-05-01 19:36:27 +00:00
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
),
],
);
}
}
2021-10-30 08:39:00 +00:00
class RecordingResult {
final String path;
final int duration;
final List<int> waveform;
2021-10-30 08:39:00 +00:00
const RecordingResult({
required this.path,
required this.duration,
required this.waveform,
2021-10-30 08:39:00 +00:00
});
factory RecordingResult.fromJson(Map<String, dynamic> json) =>
RecordingResult(
path: json['path'],
duration: json['duration'],
waveform: List<int>.from(json['waveform']),
2021-10-30 08:39:00 +00:00
);
Map<String, dynamic> toJson() => {
'path': path,
'duration': duration,
'waveform': waveform,
2021-10-30 08:39:00 +00:00
};
}