Skip to content

Commit

Permalink
feat: Send voice messages from web
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Aug 6, 2024
1 parent 3286b19 commit 47481eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions lib/pages/chat/recording_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ class RecordingDialogState extends State<RecordingDialog> {

Future<void> startRecording() async {
try {
final useOpus =
await _audioRecorder.isEncoderSupported(AudioEncoder.opus);
final codec = kIsWeb
// Web seems to create webm instead of ogg when using opus encoder
// which does not play on iOS right now. So we use wav for now:
? AudioEncoder.wav
// Everywhere else we use opus if supported by the platform:
: await _audioRecorder.isEncoderSupported(AudioEncoder.opus)
? AudioEncoder.opus
: AudioEncoder.aacLc;
fileName =
'recording${DateTime.now().microsecondsSinceEpoch}.${useOpus ? 'ogg' : 'm4a'}';
'recording${DateTime.now().microsecondsSinceEpoch}.${codec.fileExtension}';
String? path;
if (!kIsWeb) {
final tempDir = await getTemporaryDirectory();
Expand All @@ -64,7 +70,7 @@ class RecordingDialogState extends State<RecordingDialog> {
autoGain: true,
echoCancel: true,
noiseSuppress: true,
encoder: useOpus ? AudioEncoder.opus : AudioEncoder.aacLc,
encoder: codec,
),
path: path ?? '',
);
Expand Down Expand Up @@ -238,3 +244,23 @@ class RecordingResult {
required this.fileName,
});
}

extension on AudioEncoder {
String get fileExtension {
switch (this) {
case AudioEncoder.aacLc:
case AudioEncoder.aacEld:
case AudioEncoder.aacHe:
return 'm4a';
case AudioEncoder.opus:
return 'ogg';
case AudioEncoder.wav:
return 'wav';
case AudioEncoder.amrNb:
case AudioEncoder.amrWb:
case AudioEncoder.flac:
case AudioEncoder.pcm16bits:
throw UnsupportedError('Not yet used');
}
}
}
2 changes: 1 addition & 1 deletion lib/utils/platform_infos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class PlatformInfos {

static bool get usesTouchscreen => !isMobile;

static bool get platformCanRecord => (isMobile || isMacOS);
static bool get platformCanRecord => (isMobile || isMacOS || isWeb);

static String get clientName =>
'${AppConfig.applicationName} ${isWeb ? 'web' : Platform.operatingSystem}${kReleaseMode ? '' : 'Debug'}';
Expand Down

0 comments on commit 47481eb

Please sign in to comment.