Audio messages
Update time: 2024/03/07 11:13:59
NIM SDK allows users to make high-definition voice recordings and playbacks for processing audio messages.
The Flutter SDK does not support speech processing on macOS and Web.
Audio recording
NIM SDK provides the AudioService class for audio recording.
Prerequisites
The microphone permission has been granted.
Implementation
-
Register the
onAudioRecordStatusevent stream to track changes of the audio recording status. TherecordStateenumeration in theRecordInfoof the callback defines the recording states.Fieldtype Description READYEnum type The recorder is ready. The interface is used to close local audio and video playback before recording. The state is optional (not supported on Windows). STARTEnum type Start recording REACHED_MAXEnum type The audio duration reached the specified maximum recording time (not supported on Windows) SUCCESSEnum type Recording is completed successfully FAILEnum type An error occurs while recording (the parameter is not supported on Windows) CANCELEnum type The recording is canceled. dart/// Listen to the recording event NimCore.instance.audioService.onAudioRecordStatus.listen((RecordInfo recordInfo) { }); -
Start recording by calling the
startRecordmethod.Parameter Type Description recordTypeAudioOutputFormat The type of a recording file. AAC or AMR maxDurationint The maximum duration for one recording. If the maximum duration is reached, recording will stop automatically, 120 seconds by default.
dart/// Start recording. If the callsucceeds, the `READY` and `START`callback will be triggered.
/// The maximum duration for onerecording. If the maximum duration isreached, recording will stopautomatically. 120 seconds by default.
NimCore.instanceaudioServicestartRecor(AudioOutputFormat.AAC1000);
-
(Optional) Call the following methods as needed.
Method Return value type Description getAmplitudeint Get the maximum amplitude of the current recording, and update the data every 40ms. The method is not supported on Windows isAudioRecordingbool Whether recording is in progress. The method is not supported on Windows. cancelRecordbool Cancel recording `getAmplitude`dart/// Get the maximum amplitude of the current recording, and update the data every 40ms. NimCore.instance.audioService.getAmplitude();`isAudioRecording`dart/// Whether recording is in progress. NimCore.instance.audioService.isAudioRecording();`cancelRecord`dart
NimCore.instance.audioService.cancelRecord(); -
Stop recording by calling the
stopRecordmethod.dart// Stop recording NimCore.instance.audioService.stopAudioRecord();
Speech-to-text
NIM SDK allows you to convert audio files into text.
How it works:
- Record an audio file (up to 60 seconds).
- Upload the audio file to the cloud storage server and return a URL.
- Pass the URL and related parameters in the speech-to-text interface, and return the converted text.
Prerequisites
To activate the speech-to-text feature, contact the sales manager using the WhatsApp provided on the homepage of CommsEase website
Implementation
Convert speech to text by calling the voiceToText method in the messageService class.
If you have not activate the speech-to-text service, the 403 error will be returned.
dartvar result = await NimCore.instance.messageService
.voiceToText(message);






