Audio Recording and Playback

Update time: 2021/09/25 14:34:11

NIMAudio.AudioAPI provides audio play and recording feature. Before enabling audio feature, you must invoke InitModule for initialization, and then start recording after initialization.

Initialization

  • API prototype
csharp/// <summary>
/// NIM SDK initialized
/// </summary>
/// <param name="userDataParentPath">Cache directory</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool InitModule(string userDataParentPath)
  • Example
csharpNIMAudio.AudioAPI.InitModule(app_data_path);

Record

CommsEase SDK provides a set of interfaces for recording HD audio, which is used to acquire, encode and store HD audio data, and provide process callbacks. The current supported audio file formats include amr and aac.

  • API prototype

Start recording

csharp/// <summary>
/// Audio recording. android platform shall be invoked in the main thread.
/// </summary>
/// <param name="audio_format">Audio format, AAC : 0, AMR : 1</param>
/// <param name="volume">Volume (0 - 255, default 180) available to pc</param>
/// <param name="loudness">Default 0, available to pc</param>
/// <param name="capture_device">capture_device Recording device; available to pc</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool StartCapture(int audio_format,int volume,int loudness,string capture_device)

Start recording audio callback

csharp/// <summary>
/// Register the callback for starting audio recording
/// </summary>
/// <param name="cb">Callback function</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool RegStartCaptureCb(StatusCallbackDelegate cb)

Stop recording

csharp/// <summary>
/// Stop audio recording
/// </summary>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool StopCapture()

Stop recording callback

csharp/// <summary>
/// Register the callback for stopping audio recording
/// </summary>
/// <param name="cb">Callback function</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool RegStopCaptureCb(StopCaptureCallbackDelegate cb)
  • Example
csharpNIMAudio.AudioAPI.RegStartCaptureCb(StartCaptureCallback);
NIMAudio.AudioAPI.RegStopCaptureCb(StopCaptureCallback);
NIMAudio.AudioAPI.StartCapture(1, 180, 0, "");
NIMAudio.AudioAPI.StopCapture();

public void StartCaptureCallback(int rescode)
{
    if (rescode == 200)
    {
        //The interface is invoked successfully.
    }
}

public void StopCaptureCallback(int rescode, string file_path, string file_ext, int file_size, int audio_duration)
{
    //Get recording result
    //rescode: Status codes of recording results: 200 means successful recording and other values mean failed recording.
    //file_path: Storage path for audio files
    //file_ext: Postfix of audio files, amr or aac
    //file_size: Size of audio file
    //audio_duration: Audio duration, unit: ms
    string info = "StopCapture rescode:" + rescode.ToString() + "\n" +
                    "file_path:" + file_path + "\n" +
                    "file_ext:" + file_ext + "\n" +
                    "file_size:" + file_size.ToString() + "\n" +
                    "audio_duration:" + audio_duration.ToString();
    UnityEngin.Debug.Log(info);
}

Cancel recording

You can cancel recording when recording audio, the SDK will stop the current the recording and delete the saved temporary files, and the result can be obtained via callback.

  • API prototype
csharp/// <summary>
/// Cancel recording and delete temporary files
/// </summary>
/// <param name="filepath"></param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool CancelCapture()

/// <summary>
/// Register the callback for canceling audio recording
/// </summary>
/// <param name="cb">Callback function</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool RegCancelCaptureCb(StatusCallbackDelegate cb)

Play

The voice message formats of CommsEase are aac and amr. When playing, you need to input the file path and audio format, and get the playing status through registering the callback function.

  • API prototype
csharp/// <summary>
/// NIM SDK - Play, and get the status of starting play via the callback. android platform shall be invoked in the main thread.
/// </summary>
/// <param name="filePath">Absolute path of play file</param>
/// <param name="audioFormat">Format of played audio file, AAC : 0, AMR : 1</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool PlayAudio(string filePath, NIMAudioType audioFormat)

/// <summary>
/// NIM SDK - Register the callback for starting play event
/// </summary>
/// <param name="cb">Callback function for starting play event</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool RegStartPlayCb(PlayCallbackDelegate cb)

/// <summary>
/// NIM SDK - Register the callback for ending play event
/// </summary>
/// <param name="cb">Callback function for ending play event</param>
/// <returns><c>true</c> Invoked successfully, <c>false</c> Invocation failed</returns>
public static bool RegPlayEndCb(PlayCallbackDelegate cb)
  • Example
csharpNIMAudio.AudioAPI.RegStartPlayCb(StartPlayCallback);
NIMAudio.AudioAPI.RegPlayEndCb(EndPlayCallback);
NIMAudio.AudioAPI.PlayAudio("AUDIO PATH",NIMAudio.NIMAudioType.kNIMAudioAAC);

public void StartPlayCallback(int resCode,string filepath)
{
    if (resCode == 200)
    {
        //The interface is invoked successfully, and other values indicate error.
    }
}

public void EndPlayCallback(int resCode,string filepath)
{
    //Play is completed. "resCode == 200" means successful play.
}

Clear

When exiting the program, you shall invoke NIMAudio.AudioAPI.UninitModule to clear audio module and release resources.

Was this page helpful?
Yes
No
  • Initialization
  • Record
  • Cancel recording
  • Play
  • Clear