Browse SDKs · WASM
SDKsWASM

Create an audio message from a file

Create an audio message from a browser File with the WASM SDK.

Copy

createSoundMessageByFile() creates a message from a browser audio file and its metadata.

Parameters

ParameterTypeRequiredDescription
fileFileYesOriginal audio file from the browser.
uuidstringYesUnique identifier for the audio resource.
soundPathstringYesLocal filename or application-defined path of the audio file.
sourceUrlstringYesAudio URL. Pass an empty string when creating from a file; the SDK uploads the audio during sending.
dataSizenumberYesAudio size in bytes.
durationnumberYesAudio duration in seconds.
soundTypestringNoAudio MIME type.
const { data: message } = await openimsdk.createSoundMessageByFile({
  file,
  uuid: crypto.randomUUID(),
  soundPath: file.name,
  sourceUrl: '',
  dataSize: file.size,
  duration,
  soundType: file.type,
});

Derive the audio size, type, and duration from the actual file. If the browser reports the duration in milliseconds, convert it to seconds before passing it. When the Promise resolves, it has created only a pending message object. Use sendMessage() afterward to upload and send it.