fix: Record voice messages

This commit is contained in:
Krille Fear 2021-10-30 10:39:00 +02:00
parent 586380ed2f
commit f8cc559ab3
2 changed files with 36 additions and 17 deletions

View File

@ -356,13 +356,13 @@ class ChatController extends State<Chat> {
void voiceMessageAction() async {
if (await Record().hasPermission() == false) return;
final result = await showDialog<Map>(
final result = await showDialog<RecordingResult>(
context: context,
useRootNavigator: false,
builder: (c) => const RecordingDialog(),
);
if (result == null) return;
final audioFile = File(result['path']);
final audioFile = File(result.path);
final file = MatrixAudioFile(
bytes: audioFile.readAsBytesSync(),
name: audioFile.path,
@ -373,7 +373,7 @@ class ChatController extends State<Chat> {
room.sendFileEvent(file, inReplyTo: replyEvent, extraContent: {
'info': {
...file.info,
'duration': result['duration'],
'duration': result.duration,
}
}),
);

View File

@ -73,6 +73,16 @@ class _RecordingDialogState extends State<RecordingDialog> {
super.dispose();
}
void _stopAndSend() async {
_recorderSubscription?.cancel();
await _audioRecorder.stop();
Navigator.of(context, rootNavigator: false)
.pop<RecordingResult>(RecordingResult(
path: _recordedPath,
duration: _duration.inMilliseconds,
));
}
@override
Widget build(BuildContext context) {
const maxDecibalWidth = 64.0;
@ -130,14 +140,7 @@ class _RecordingDialogState extends State<RecordingDialog> {
),
if (error != true)
CupertinoDialogAction(
onPressed: () async {
_recorderSubscription?.cancel();
await _audioRecorder.stop();
Navigator.of(context, rootNavigator: false).pop<Map>({
'path': _recordedPath,
'duration': _duration.inMilliseconds,
});
},
onPressed: _stopAndSend,
child: Text(L10n.of(context).send.toUpperCase()),
),
],
@ -157,12 +160,7 @@ class _RecordingDialogState extends State<RecordingDialog> {
),
if (error != true)
TextButton(
onPressed: () async {
_recorderSubscription?.cancel();
await _audioRecorder.stop();
Navigator.of(context, rootNavigator: false)
.pop<String>(_recordedPath);
},
onPressed: _stopAndSend,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
@ -176,3 +174,24 @@ class _RecordingDialogState extends State<RecordingDialog> {
);
}
}
class RecordingResult {
final String path;
final int duration;
const RecordingResult({
@required this.path,
@required this.duration,
});
factory RecordingResult.fromJson(Map<String, dynamic> json) =>
RecordingResult(
path: json['path'],
duration: json['duration'],
);
Map<String, dynamic> toJson() => {
'path': path,
'duration': duration,
};
}