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

View File

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