Audio recording and playback

Update time: 2021/12/03 09:22:46

Audio recording and playback

Initialization and clearup

When starting the program, you must invoke the interface LoadLibrary to load nim\_audio.dll, and then invoke the interface nim\_audio\_init\_module to initialize audio module. Before leaving the program, you must invoke the interface nim\_audio\_uninit\_module to release audio module, and then invoke the interface FreeLibrary to release dll.

Get audio capture devices

By usinggetting audio capture device, you can specify the capture device when invoking the interface for Audio capture, nim_audio.h:

	NIM_SDK_DLL_API bool nim_audio_enum_capture_device();

The result can be acquired using the registered callback interface:

	NIM_SDK_DLL_API bool nim_audio_reg_enum_capture_device_cb(nim_enum_capture_device_cb cb);

C++

	void OnEnumCaptureDeviceCallback(int rescode, const wchar_t* device_list)
	{
		···
	}
	
	void foo()
	{
		nim_audio::Audio::RegEnumCaptureDeviceCb(&OnEnumCaptureDeviceCallback);
		nim_audio::Audio::EnumCaptureDevice();
	}

C#
 
    private void OnEnumDevices(int resCode, string deviecs)
    {
        _outputTools.ShowInfo("Get devices:\r\nresCode:{0}\r\ndevices:{1}", resCode, deviecs);
    }

	public void foo()
    {
        //Register the result callback for getting the list of devices
        NIMAudio.AudioAPI.RegEnumDevicesCb(OnEnumDevices);
        NIMAudio.AudioAPI.EnumCaptureDevices();
    }

C

	void OnEnumCaptureDeviceCallback(int rescode, const wchar_t* device_list)
	{
		···
	}
	
	typedef bool(*nim_audio_enum_capture_device)();
	typedef bool(*nim_audio_reg_enum_capture_device_cb)(nim_enum_capture_device_cb cb);
	
	void foo()
	{
		nim_audio_enum_capture_device enum_func = (nim_audio_enum_capture_device) GetProcAddress(hInst, "nim_audio_enum_capture_device");
		nim_audio_reg_enum_capture_device_cb reg_func = (nim_audio_reg_enum_capture_device_cb) GetProcAddress(hInst, "nim_audio_reg_enum_capture_device_cb");
	
		reg_func(&OnEnumCaptureDeviceCallback);
		enum_func();
	}

Audio capture

After initializing the audio module, you can invoke the following interface to register the callback feature related to Audio capture:

	typedef bool(*nim_audio_reg_start_capture_cb)(nim_rescode_cb cb);
	typedef bool(*nim_audio_reg_stop_capture_cb)(nim_stop_capture_cb cb);
	typedef bool(*nim_audio_reg_cancel_audio_cb)(nim_rescode_cb cb);

You can invoke the following interface to implement Audio capture, and ensure preparation of audio device before acquisition.

	typedef bool(*nim_audio_start_capture)(const char* call_id, const char* res_id, int audio_format, int volume, const wchar_t* capture_device);
	typedef bool(*nim_audio_stop_capture)();
	typedef bool(*nim_audio_cancel_audio)(const wchar_t* file_path);

C++
	
	void OnStartCaptureCallback(int code)
	{
		···
	}
	
	void OnStopCaptureCallback(int rescode, const char* sid, const char* cid, const char* file_path, const char* file_ext, long file_size, int audio_duration)
	{
		···
	}
	
	void OnCancelCaptureCallback(int code)
	{
		···
	}

	void OnInit()
	{	
		nim_audio::Audio::RegStartCaptureCb(&OnStartCaptureCallback);
		nim_audio::Audio::RegStopCaptureCb(&OnStopCaptureCallback);
		nim_audio::Audio::RegCancelAudioCb(&OnCancelCaptureCallback);
	}
	
	bool StartCapture(const std::string session_id, const std::string msg_id, nim_audio::nim_audio_type audio_format /*= nim_audio::AAC*/, int volume /*= 180*/, const wchar_t* capture_device /*= nullptr*/)
	{
		return nim_audio::Audio::StartCapture(session_id.c_str(), msg_id.c_str(), audio_format, volume, capture_device);
	}
	
	bool StopCapture()
	{
		return nim_audio::Audio::StopCapture();
	}
	
	bool CancelCapture(const std::wstring file_path)
	{
		return nim_audio::Audio::CancelAudio(file_path.c_str());
	}

C#

    private void OnCaptureStarted(int resCode)
    {
        _outputTools.ShowInfo("Start recording:{0}", resCode); ;
    }
    //Register the result callback for starting Audio capture
    NIMAudio.AudioAPI.RegStartAudioCaptureCb(OnCaptureStarted);
    //Start Audio capture
	NIMAudio.AudioAPI.StartCapture("", "", _audioType);
    
    private void OnCaptureStopped(int resCode, string call_id, string res_id, wstring file_path, string file_ext, int file_size, int audio_duration)
    {
        _outputTools.ShowInfo("End recording:{0}\r\n file_path:{1}\r\n ext:{2}\r\n size:{3}\r\n duration:{4}",
            resCode, file_path, file_ext, file_size, audio_duration);
    }
    //Register the result callback for stopping audi capture
    NIMAudio.AudioAPI.RegStopAudioCaptureCb(OnCaptureStopped);
    //Stop capturing audios
    NIMAudio.AudioAPI.StopCapture();

    //Register the result callback for canceling audio capture
    NIMAudio.AudioAPI.RegCancelAudioCapturingCb(NIMAudio.NIMResCodeCb cb)
    / Cancel recording and delete temporary files
    NIMAudio.AudioAPI.CancelCapture(strings audioPath)
C

	void OnStartCaptureCallback(int code)
	{
		···
	}
	
	void OnStopCaptureCallback(int rescode, const char* sid, const char* cid, const char* file_path, const char* file_ext, long file_size, int audio_duration)
	{
		···
	}
	
	void OnCancelCaptureCallback(int code)
	{
		···
	}

	typedef bool(*nim_audio_reg_start_capture_cb)(nim_rescode_cb cb);
	typedef bool(*nim_audio_reg_stop_capture_cb)(nim_stop_capture_cb cb);
	typedef bool(*nim_audio_reg_cancel_audio_cb)(nim_rescode_cb cb);

	typedef bool(*nim_audio_start_capture)(const char* call_id, const char* res_id, int audio_format, int volume, const wchar_t* capture_device);
	typedef bool(*nim_audio_stop_capture)();
	typedef bool(*nim_audio_cancel_audio)(const char* file_path);

	void OnInit()
	{
		nim_audio_reg_start_capture_cb start_func = (nim_audio_reg_start_capture_cb) GetProcAddress(hInst, "nim_audio_reg_start_capture_cb");
		nim_audio_reg_stop_capture_cb stop_func = (nim_audio_reg_stop_capture_cb) GetProcAddress(hInst, "nim_audio_reg_stop_capture_cb");
		nim_audio_reg_cancel_audio_cb cancel_func = (nim_audio_reg_cancel_audio_cb) GetProcAddress(hInst, "nim_audio_reg_cancel_audio_cb");
			
		nim_audio_reg_stop_capture_cb stop_func = (nim_audio_reg_stop_capture_cb) GetProcAddress(hInst, "nim_audio_reg_stop_capture_cb");
		nim_audio_reg_cancel_audio_cb cancel_func = (nim_audio_reg_cancel_audio_cb) GetProcAddress(hInst, "nim_audio_reg_cancel_audio_cb");
			
		start_funcb(&OnStartCaptureCallback);
		stop_func(&OnStopCaptureCallback);
		cancel_func(&OnCancelCaptureCallback);
	}

	bool StartCapture(const char *session_id, const char *msg_id, nim_audio::nim_audio_type audio_format, int volume, const wchar_t* capture_device)
	{
		nim_audio_start_capture func = (nim_audio_start_capture) GetProcAddress(hInst, "nim_audio_start_capture");
		return func(session_id, msg_id, audio_format, volume, capture_device);
	}
	
	bool StopCapture()
	{
		nim_audio_stop_capture func = (nim_audio_stop_capture) GetProcAddress(hInst, "nim_audio_stop_capture");
		return func(session_id);
	}
	
	bool CancelCapture(const char *file_path)
	{
		nim_audio_reg_cancel_audio_cb func = (nim_audio_reg_cancel_audio_cb) GetProcAddress(hInst, "nim_audio_reg_cancel_audio_cb");
		return func(file_path);
	}

Start or Stop audio playback

The nim\_audio\_reg\_start\_play\_cb interface is invoked to register the callback feature for starting audio playback. The nim\_audio\_reg\_stop\_play\_cb interface is invoked to register the callback for stopping audio playback. The nim\_audio\_play\_audio interface is invoked to play an audio file. The nim\_audio\_stop\_play\_audio interface is invoked to stop playback.
Was this page helpful?
Yes
No
  • Audio recording and playback
  • Initialization and clearup
  • Get audio capture devices
  • Audio capture
  • Start or Stop audio playback