语音录制与播放

初始化与清理

在程序启动时,调用LoadLibrary 加载nim\_audio.dll;然后调用接口nim\_audio\_init\_module 初始化语音模块;在退出程序前,先调用接口nim\_audio\_uninit\_module 释放语音模块,然后调用FreeLibrary 释放dll。

获取语音采集设备

通过获取语音采集设备,调用语音采集接口时可以指定采集设备,nim_audio.h:

    NIM_SDK_DLL_API bool nim_audio_enum_capture_device();

结果通过注册的回调接口获取:

    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("获取设备:\r\nresCode:{0}\r\ndevices:{1}", resCode, deviecs);
    }

    public void foo()
    {
        //注册获取设备列表结果回调
        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();
    }

语音采集

语音模块初始化后调用如下接口注册语音采集相关的回调函数:

    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 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("开始录制:{0}", resCode); ;
    }
    //注册开始语音采集的结果回调
    NIMAudio.AudioAPI.RegStartAudioCaptureCb(OnCaptureStarted);
    //开始语音采集
    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("录制结束:{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);
    }
    //注册停止采集的结果回调
    NIMAudio.AudioAPI.RegStopAudioCaptureCb(OnCaptureStopped);
    //停止采集
    NIMAudio.AudioAPI.StopCapture();

    //注册取消采集的结果回调
    NIMAudio.AudioAPI.RegCancelAudioCapturingCb(NIMAudio.NIMResCodeCb cb)
    //取消录制并删除临时文件
    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);
    }

语音播放与停止

调用接口nim\_audio\_reg\_start\_play\_cb 注册语音播放开始回调函数,调用接口nim\_audio\_reg\_stop\_play\_cb 注册语音播放结束回调函数。调用接口nim\_audio\_play\_audio 播放语音。调用接口nim\_audio\_stop\_play\_audio 停止播放。