feat: Record voice message with opus/ogg if supported
This commit is contained in:
parent
0bd61ced3f
commit
a7473af40f
6 changed files with 21 additions and 51 deletions
|
|
@ -6,7 +6,7 @@ import 'package:flutter/material.dart';
|
|||
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:nyx_converter/nyx_converter.dart';
|
||||
import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import 'package:fluffychat/utils/error_reporter.dart';
|
||||
|
|
@ -77,27 +77,11 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
|||
if (Platform.isIOS &&
|
||||
matrixFile.mimeType.toLowerCase() == 'audio/ogg') {
|
||||
Logs().v('Convert ogg audio file for iOS...');
|
||||
final convertedFile = File('${file.path}.aac');
|
||||
if (await convertedFile.exists()) {
|
||||
file = convertedFile;
|
||||
} else {
|
||||
final completer = Completer<File>();
|
||||
NyxConverter.convertTo(
|
||||
file.path,
|
||||
tempDir.path,
|
||||
fileName: '${fileName}_${matrixFile.name}',
|
||||
container: NyxContainer.aac,
|
||||
execution: (
|
||||
String? path,
|
||||
NyxStatus status, {
|
||||
String? errorMessage,
|
||||
}) {
|
||||
if (path != null) completer.complete(File(path));
|
||||
if (errorMessage != null) completer.completeError(errorMessage);
|
||||
},
|
||||
);
|
||||
file = await completer.future;
|
||||
final convertedFile = File('${file.path}.caf');
|
||||
if (await convertedFile.exists() == false) {
|
||||
OpusCaf().convertOpusToCaf(file.path, convertedFile.path);
|
||||
}
|
||||
file = convertedFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import 'package:fluffychat/utils/platform_infos.dart';
|
|||
import 'events/audio_player.dart';
|
||||
|
||||
class RecordingDialog extends StatefulWidget {
|
||||
static const String recordingFileType = 'm4a';
|
||||
const RecordingDialog({
|
||||
super.key,
|
||||
});
|
||||
|
|
@ -36,9 +35,11 @@ class RecordingDialogState extends State<RecordingDialog> {
|
|||
|
||||
Future<void> startRecording() async {
|
||||
try {
|
||||
final useOpus =
|
||||
await _audioRecorder.isEncoderSupported(AudioEncoder.opus);
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final path = _recordedPath =
|
||||
'${tempDir.path}/recording${DateTime.now().microsecondsSinceEpoch}.${RecordingDialog.recordingFileType}';
|
||||
'${tempDir.path}/recording${DateTime.now().microsecondsSinceEpoch}.${useOpus ? 'ogg' : 'm4a'}';
|
||||
|
||||
final result = await _audioRecorder.hasPermission();
|
||||
if (result != true) {
|
||||
|
|
@ -46,14 +47,16 @@ class RecordingDialogState extends State<RecordingDialog> {
|
|||
return;
|
||||
}
|
||||
await WakelockPlus.enable();
|
||||
|
||||
await _audioRecorder.start(
|
||||
const RecordConfig(
|
||||
RecordConfig(
|
||||
bitRate: bitRate,
|
||||
sampleRate: samplingRate,
|
||||
numChannels: 1,
|
||||
autoGain: true,
|
||||
echoCancel: true,
|
||||
noiseSuppress: true,
|
||||
encoder: useOpus ? AudioEncoder.opus : AudioEncoder.aacLc,
|
||||
),
|
||||
path: path,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue