Audio messages
Update time: 2023/02/01 16:05:42
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
onAudioRecordStatus
event stream to track changes of the audio recording status. TherecordState
enumeration in theRecordInfo
of the callback defines the recording states.Fieldtype Description READY
Enum 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). START
Enum type Start recording REACHED_MAX
Enum type The audio duration reached the specified maximum recording time (not supported on Windows) SUCCESS
Enum type Recording is completed successfully FAIL
Enum type An error occurs while recording (the parameter is not supported on Windows) CANCEL
Enum type The recording is canceled. /// Listen to the recording event NimCore.instance.audioService.onAudioRecordStatus.listen((RecordInfo recordInfo) { });
-
Start recording by calling the
startRecord
method.Parameter Type Description recordType
AudioOutputFormat The type of a recording file. AAC or AMR maxDuration
int The maximum duration for one recording. If the maximum duration is reached, recording will stop automatically, 120 seconds by default.
/// 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 getAmplitude
int Get the maximum amplitude of the current recording, and update the data every 40ms. The method is not supported on Windows isAudioRecording
bool Whether recording is in progress. The method is not supported on Windows. cancelRecord
bool Cancel recording `getAmplitude`/// Get the maximum amplitude of the current recording, and update the data every 40ms. NimCore.instance.audioService.getAmplitude();
`isAudioRecording`/// Whether recording is in progress. NimCore.instance.audioService.isAudioRecording();
`cancelRecord`NimCore.instance.audioService.cancelRecord();
-
Stop recording by calling the
stopRecord
method.// 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.
var result = await NimCore.instance.messageService
.voiceToText(message);