1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 04:49:21 +00:00

Fix audio record

This commit is contained in:
Pierre HUBERT 2021-03-12 21:19:40 +01:00
parent e7b1beca50
commit ece9164d93
4 changed files with 60 additions and 13 deletions

View File

@ -4,11 +4,13 @@ import 'dart:typed_data';
import 'package:comunic/ui/dialogs/alert_dialog.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:comunic/utils/permission_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:record/record.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:record_mp3/record_mp3.dart';
import 'package:video_player/video_player.dart';
/// Record audio dialog
@ -17,9 +19,9 @@ import 'package:video_player/video_player.dart';
/// Record audio
Future<Uint8List> showRecordAudioDialog(BuildContext context) async {
if (!await Record.hasPermission()) {
await alert(
context, "Permission d'accéder au périphérique audio refusée !");
// Request record permission
if (!await requestPermission(context, Permission.microphone)) {
alert(context, tr("Did not get permission to access microphone!"));
return null;
}
@ -32,8 +34,6 @@ Future<Uint8List> showRecordAudioDialog(BuildContext context) async {
barrierDismissible: false,
);
if (await Record.isRecording()) await Record.stop();
return res;
}
@ -67,6 +67,7 @@ class __RecordAudioDialogState extends State<_RecordAudioDialog> {
@override
void dispose() {
_disposePlayer();
RecordMp3.instance.stop();
super.dispose();
}
@ -168,9 +169,12 @@ class __RecordAudioDialogState extends State<_RecordAudioDialog> {
final dir = await getTemporaryDirectory();
_recordPath = path.join(dir.absolute.path, "tmp-audio-record.m4a");
_recordPath = path.join(dir.absolute.path, "tmp-audio-record.mp3");
await Record.start(path: _recordPath);
RecordMp3.instance.start(_recordPath, (fail) {
print(fail);
snack(context, tr("Failed to start recording!"));
});
setState(() => _recording = true);
} catch (e, s) {
@ -181,7 +185,7 @@ class __RecordAudioDialogState extends State<_RecordAudioDialog> {
void _stopRecording() async {
try {
await Record.stop();
RecordMp3.instance.stop();
setState(() => _recording = false);
} catch (e, s) {

View File

@ -0,0 +1,26 @@
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
/// Permission utilities
///
/// @author Pierre Hubert
/// Request a permission, if it has not been granted yet
Future<bool> requestPermission(
BuildContext context, Permission permission) async {
try {
if (await permission.isPermanentlyDenied) {
return false;
}
if (await permission.isGranted) return true;
if (await permission.request() != PermissionStatus.granted) return false;
return true;
} catch (e, s) {
logError(e, s);
return false;
}
}

View File

@ -415,6 +415,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.2"
permission_handler:
dependency: "direct main"
description:
name: permission_handler
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.0+2"
permission_handler_platform_interface:
dependency: transitive
description:
name: permission_handler_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
petitparser:
dependency: transitive
description:
@ -464,13 +478,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
record:
record_mp3:
dependency: "direct main"
description:
name: record
name: record_mp3
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
version: "2.1.0"
rxdart:
dependency: transitive
description:

View File

@ -113,7 +113,10 @@ dependencies:
video_thumbnail: ^0.2.5+1
# Record audio file
record: ^1.0.2
record_mp3: ^2.1.0
# Request permissions
permission_handler: ^5.1.0+2
dev_dependencies:
flutter_test: