@tencentcloud/sdk-mcp 1.7.7 → 1.7.9-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +7 -9
- package/index.js +8 -8
- package/knowledge/chunker.js +1 -0
- package/knowledge/doc-url-resolver.js +16 -9
- package/knowledge/embedding/build-vectors.js +92 -0
- package/knowledge/embedding/embed-model.js +41 -0
- package/knowledge/embedding/vector-augment.js +126 -0
- package/knowledge/embedding/vector-index.js +70 -0
- package/knowledge/frontmatter-meta.js +23 -0
- package/knowledge/path-meta.js +56 -0
- package/knowledge/search/exact-lookup.js +145 -16
- package/knowledge/search/filter-pool.js +77 -33
- package/knowledge/search/rank-profile.js +25 -0
- package/knowledge/search/requires-framework.js +4 -1
- package/knowledge/search/search-core.js +74 -13
- package/knowledge/search/search-knowledge.js +24 -34
- package/knowledge/search/search-pass.js +495 -169
- package/knowledge/search/url-row-scorer.js +24 -6
- package/package.json +3 -1
- package/resource/index/manifest.json +1 -1
- package/resource/index/vectors/index.json +4 -0
- package/resource/integration/callkit/preset/android/integration.md +0 -2
- package/resource/integration/callkit/preset/ios/integration.md +0 -2
- package/resource/integration/chat/preset/react/integration.md +9 -5
- package/resource/integration/chat/preset/uni-app/integration.md +4 -0
- package/resource/integration/chat/preset/vue/integration.md +10 -6
- package/resource/integration/livekit/preset/live/android/prepare-integration.md +0 -4
- package/resource/integration/livekit/preset/live/ios/prepare-integration.md +0 -4
- package/resource/integration/roomkit/preset/vue/integration.md +154 -208
- package/resource/knowledge/callkit/best-practice/android/video-chat-dating.md +0 -2
- package/resource/knowledge/callkit/core-sdk/android/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/android/make-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/flutter/answer-call.md +0 -4
- package/resource/knowledge/callkit/core-sdk/flutter/make-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/flutter/offline-push.md +0 -2
- package/resource/knowledge/callkit/core-sdk/ios/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/ios/make-call.md +0 -4
- package/resource/knowledge/callkit/core-sdk/ios/offline-push.md +0 -2
- package/resource/knowledge/callkit/core-sdk/uni-app/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/uni-app/make-call.md +0 -2
- package/resource/knowledge/callkit/faq/ios.md +0 -2
- package/resource/knowledge/callkit/faq/uniapp-app.md +0 -2
- package/resource/knowledge/callkit/preset/miniprogram/integration.md +2 -3
- package/resource/knowledge/callkit/preset/miniprogram/uniapp-integration.md +0 -2
- package/resource/knowledge/chat/preset/ios-uikit/integration.md +0 -16
- package/resource/knowledge/chat/preset/react/h5/integration.md +16 -5
- package/resource/knowledge/chat/preset/vue/features/conversation-list.md +6 -3
- package/resource/knowledge/chat/preset/vue/h5/integration.md +7 -4
- package/resource/knowledge/iot/app/activate-service.md +95 -0
- package/resource/knowledge/iot/app/demo/android.md +93 -0
- package/resource/knowledge/iot/app/demo/ios.md +87 -0
- package/resource/knowledge/iot/app/demo/miniprogram.md +79 -0
- package/resource/knowledge/iot/app/device-binding.md +378 -0
- package/resource/knowledge/iot/app/device-sharing.md +414 -0
- package/resource/knowledge/iot/app/family-management.md +762 -0
- package/resource/knowledge/iot/app/integration/android.md +328 -0
- package/resource/knowledge/iot/app/integration/harmonyos.md +326 -0
- package/resource/knowledge/iot/app/integration/ios.md +351 -0
- package/resource/knowledge/iot/app/integration/miniprogram.md +268 -0
- package/resource/knowledge/iot/app/monitor-playback.md +557 -0
- package/resource/knowledge/iot/app/remote-control.md +139 -0
- package/resource/knowledge/iot/app/video-call.md +559 -0
- package/resource/knowledge/iot/app/wechat-call/device-call-miniprogram.md +204 -0
- package/resource/knowledge/iot/app/wechat-call/miniprogram-call-device.md +193 -0
- package/resource/knowledge/iot/client-api/app/android/txiotcallsession.md +189 -0
- package/resource/knowledge/iot/client-api/app/android/txiotcloudstorage.md +77 -0
- package/resource/knowledge/iot/client-api/app/android/txiotdevicemanager.md +175 -0
- package/resource/knowledge/iot/client-api/app/android/txiotengine.md +172 -0
- package/resource/knowledge/iot/client-api/app/android/txiotenginedef.md +163 -0
- package/resource/knowledge/iot/client-api/app/android/txiotfamilymanager.md +146 -0
- package/resource/knowledge/iot/client-api/app/android/txiotmonitorsession.md +246 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotcallsession.md +191 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotcloudstorage.md +77 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotdevicemanager.md +183 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotengine.md +186 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotenginedef.md +164 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotfamilymanager.md +150 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotmonitorsession.md +252 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotcallsession.md +199 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotcloudstorage.md +76 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotdevicemanager.md +173 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotengine.md +173 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotenginedef.md +171 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotfamilymanager.md +146 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotmonitorsession.md +250 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotcallsession.md +180 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotcloudstorage.md +76 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotdevicemanager.md +281 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotengine.md +183 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotenginedef.md +152 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotfamilymanager.md +179 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotmonitorsession.md +226 -0
- package/resource/knowledge/iot/client-api/device/android-smart-device.md +813 -0
- package/resource/knowledge/iot/client-api/device/linux-rtos-device.md +2027 -0
- package/resource/knowledge/iot/device/activate-service.md +167 -0
- package/resource/knowledge/iot/device/ai-voice-chat/advanced-features/custom-agent.md +171 -0
- package/resource/knowledge/iot/device/ai-voice-chat/advanced-features/smart-device-control.md +721 -0
- package/resource/knowledge/iot/device/ai-voice-chat/integration-guide/android-smart-device.md +247 -0
- package/resource/knowledge/iot/device/ai-voice-chat/integration-guide/linux-rtos-device.md +368 -0
- package/resource/knowledge/iot/device/ai-voice-chat/overview.md +84 -0
- package/resource/knowledge/iot/device/cloud-storage.md +696 -0
- package/resource/knowledge/iot/device/data-model.md +972 -0
- package/resource/knowledge/iot/device/demo/android-smart-device.md +97 -0
- package/resource/knowledge/iot/device/demo/linux-rtos-device.md +106 -0
- package/resource/knowledge/iot/device/device-ota-upgrade.md +237 -0
- package/resource/knowledge/iot/device/dynamic-registration/android-smart-device.md +275 -0
- package/resource/knowledge/iot/device/dynamic-registration/linux-rtos-device.md +297 -0
- package/resource/knowledge/iot/device/login-and-register/android-smart-device.md +131 -0
- package/resource/knowledge/iot/device/login-and-register/linux-rtos-device.md +291 -0
- package/resource/knowledge/iot/device/monitor-pushing/android-smart-device.md +237 -0
- package/resource/knowledge/iot/device/monitor-pushing/linux-rtos-device.md +493 -0
- package/resource/knowledge/iot/device/video-call/android-smart-device.md +299 -0
- package/resource/knowledge/iot/device/video-call/linux-rtos-device.md +465 -0
- package/resource/knowledge/iot/device/wechat-call/android-smart-device.md +534 -0
- package/resource/knowledge/iot/device/wechat-call/linux-rtos-device.md +681 -0
- package/resource/knowledge/iot/kids-watch/overview.md +28 -0
- package/resource/knowledge/iot/security-camera/activate-service.md +74 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/app-integration.md +302 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/device-integration/android.md +237 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/device-integration/linux-rtos.md +493 -0
- package/resource/knowledge/iot/security-camera/overview.md +31 -0
- package/resource/knowledge/iot/security-camera/ptz-control.md +153 -0
- package/resource/knowledge/iot/security-camera/recording-and-snapshot.md +182 -0
- package/resource/knowledge/iot/security-camera/voice-intercom.md +174 -0
- package/resource/knowledge/iot/smart-vision/serviceoverview.md +114 -0
- package/resource/knowledge/iot/voice-recorder/app-demo.md +99 -0
- package/resource/knowledge/iot/voice-recorder/device-demo.md +189 -0
- package/resource/knowledge/iot/voice-recorder/overview.md +64 -0
- package/resource/knowledge/livekit/core-sdk/features/android/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/android/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/android/live-list.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/audio-effect.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/ios/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/ios/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/ios/live-list.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/uni-app/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/uni-app/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/android/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/android/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/android/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/flutter/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/flutter/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/flutter/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/ios/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/ios/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/ios/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/uni-app/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/uni-app/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/uni-app/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/android/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/android/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/android/integration.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/flutter/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/ios/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/ios/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/ios/integration.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/uni-app/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/uni-app/integration.md +0 -2
- package/resource/knowledge/livekit/faq/error-code.md +0 -2
- package/resource/knowledge/push/config/android/honor.md +16 -1
- package/resource/knowledge/push/config/android/huawei.md +20 -4
- package/resource/knowledge/push/config/android/meizu.md +19 -5
- package/resource/knowledge/push/config/android/vivo.md +20 -1
- package/resource/knowledge/push/config/donut.md +0 -2
- package/resource/knowledge/push/config/harmonyos.md +21 -5
- package/resource/knowledge/push/integration/android.md +0 -5
- package/resource/knowledge/push/integration/donut.md +0 -3
- package/resource/knowledge/push/integration/flutter.md +1 -4
- package/resource/knowledge/push/integration/harmonyos.md +0 -3
- package/resource/knowledge/push/integration/ios.md +0 -3
- package/resource/knowledge/push/integration/react-native.md +0 -5
- package/resource/knowledge/push/integration/uni-app.md +0 -3
- package/resource/knowledge/push/integration/unity.md +0 -3
- package/resource/knowledge/push/integration/unreal-engine.md +0 -3
- package/resource/knowledge/roomkit/core-sdk/android/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/vue/room-view.md +0 -3
- package/resource/knowledge/roomkit/preset/uniapp-app/integration.md +18 -14
- package/resource/knowledge/rtcengine/web/how_to_run_web_sdk_demo.md +0 -2
- package/resource/knowledge/rtcengine/web/plugin/23-advanced-support-detection.md +30 -0
- package/resource/knowledge/rtcengine/web/plugin/24-advanced-network-quality.md +0 -2
- package/resource/knowledge/rtcengine/web/plugin/31-advanced-plugins-overview.md +1 -1
- package/resource/knowledge/rtcengine/web/plugin/35-advanced-ai-denoiser.md +1 -1
- package/resource/knowledge/rtcengine/web/plugin/36-advanced-virtual-background.md +4 -0
- package/resource/knowledge/trtc/android/TRTCCloud.md +94 -85
- package/resource/knowledge/trtc/android/TRTCCloudDef.md +48 -46
- package/resource/knowledge/trtc/android/TRTCCloudListener.md +48 -48
- package/resource/knowledge/trtc/android/TXAudioEffectManager.md +18 -18
- package/resource/knowledge/trtc/android/TXDeviceManager.md +4 -4
- package/resource/knowledge/trtc/android/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/c++/ITRTCCloud.md +90 -90
- package/resource/knowledge/trtc/c++/ITXAudioEffectManager.md +18 -18
- package/resource/knowledge/trtc/c++/ITXDeviceManager.md +9 -9
- package/resource/knowledge/trtc/c++/TRTCCloudCallback.md +47 -47
- package/resource/knowledge/trtc/c++/TRTCTypeDef.md +47 -45
- package/resource/knowledge/trtc/c++/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/ios/TRTCCloud.md +93 -93
- package/resource/knowledge/trtc/ios/TRTCCloudDelegate.md +50 -50
- package/resource/knowledge/trtc/ios/TXAudioEffectManager.md +8 -8
- package/resource/knowledge/trtc/ios/TXDeviceManager.md +9 -9
- package/resource/knowledge/trtc/ios/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/ios/TXLiveSDKTypeDef.md +48 -46
- package/server-config.js +8 -9
- package/server.js +1 -1
- package/tools/common/finalize_answer.js +1 -1
- package/tools/common/present_framework_choice.js +1 -1
- package/tools/common/search_trtc_knowledge.js +22 -3
- package/tools/definitions/search_trtc_knowledge_def.js +3 -2
- package/utils/preflight-route.js +19 -2
- package/utils/report-cls-client.js +1 -1
- package/utils/report-es-client.js +1 -1
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
---
|
|
2
|
+
product: iot
|
|
3
|
+
framework:
|
|
4
|
+
- android
|
|
5
|
+
types:
|
|
6
|
+
- feature
|
|
7
|
+
retrievable_scope: always
|
|
8
|
+
feature_domain: device
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
本文将介绍设备如何在 Android 平台接入 **AI 语音对话** 能力,实现与云端智能体的**实时语音交互**、**文本消息收发**、**对话打断**以及**语义触发联系人呼叫**,帮助您快速了解 AI 语音对话的功能。
|
|
12
|
+
|
|
13
|
+
## 前提条件
|
|
14
|
+
|
|
15
|
+
在开始之前,请您先完成以下步骤,若您已经完成对应的步骤,可跳过:
|
|
16
|
+
1. 您需要先 [注册腾讯云](https://cloud.tencent.com/document/product/378/17985) 账号,并完成 [实名认证](https://cloud.tencent.com/document/product/378/3629)。
|
|
17
|
+
|
|
18
|
+
2. 购买了 [腾讯云物联网开发平台](https://buy.cloud.tencent.com/iotexplorer) 相关实例,开通并激活了 智能语音增值服务。
|
|
19
|
+
|
|
20
|
+
3. 在 [物联网开发平台控制台](https://console.cloud.tencent.com/iotexplorer/v2) 拿到了本 Demo 所需要的设备三元组(**已经激活过了**)。
|
|
21
|
+
|
|
22
|
+
4. 完成 登录与注册 文档中的跑通登录功能。
|
|
23
|
+
|
|
24
|
+
> **说明:**
|
|
25
|
+
>
|
|
26
|
+
> - **AI 对话服务属于增值服务,必须开通此增值服务后激活设备 license 才可以正常使用。**
|
|
27
|
+
> - AI 语音对话依赖麦克风采集与扬声器播放,请确保已在 `AndroidManifest.xml` 中声明 `android.permission.RECORD_AUDIO` 权限,并在运行时向用户申请授权,同时请确认该设备所属产品已在控制台开通 AI 对话服务,否则将返回 `TXIoTError.AITALK_NOT_ACTIVATED`。
|
|
28
|
+
|
|
29
|
+
## 接入步骤
|
|
30
|
+
|
|
31
|
+
### 步骤 1:注册回调
|
|
32
|
+
|
|
33
|
+
登录成功后,通过 `TXIoTAITalkSession.getInstance().addListener` 注册 TXIoTAITalkSession.Listener,AI 语音对话相关的回调如下:
|
|
34
|
+
|
|
35
|
+
|回调|说明|
|
|
36
|
+
|---------|---------|
|
|
37
|
+
|`onReceiveAsrText`|收到用户语音的 ASR 识别结果文本时触发,可用于在屏幕上展示用户说话内容。|
|
|
38
|
+
|`onReceiveBotText`|收到智能体回复的文本时触发,可用于在屏幕上展示机器人的应答内容。|
|
|
39
|
+
|`onBotStateChanged`|智能体状态发生变化时触发,携带 `oldState` 与 `newState`(。|
|
|
40
|
+
|`onLaunchCall`|智能体根据语义识别到需要呼叫某位联系人时触发,携带 `Contact`(联系人信息),设备可据此发起音视频通话(需先注册联系人。|
|
|
41
|
+
|`onError`|AI 对话发生错误时触发,携带 `errorCode` 与 `errorMsg`,常见错误码。|
|
|
42
|
+
|
|
43
|
+
``` java
|
|
44
|
+
TXIoTAITalkSession.Listener listener = new TXIoTAITalkSession.Listener() {
|
|
45
|
+
@Override
|
|
46
|
+
public void onReceiveAsrText(String text) {
|
|
47
|
+
// 用户语音的识别结果,可显示到屏幕
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Override
|
|
51
|
+
public void onReceiveBotText(String text) {
|
|
52
|
+
// 智能体回复的文本,可显示到屏幕
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@Override
|
|
56
|
+
public void onBotStateChanged(TXIoTAITalkSession.BotState oldState,
|
|
57
|
+
TXIoTAITalkSession.BotState newState) {
|
|
58
|
+
// 根据 newState 更新设备指示灯、屏幕或其他交互状态
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Override
|
|
62
|
+
public void onLaunchCall(TXIoTAITalkSession.Contact contact) {
|
|
63
|
+
// 智能体语义触发联系人呼叫,可据此发起音视频通话
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Override
|
|
67
|
+
public void onError(int errorCode, String errorMsg) {
|
|
68
|
+
// 记录 errorCode 和 errorMsg,并停止或重试业务流程
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
TXIoTAITalkSession.getInstance().addListener(listener);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 步骤 2:参数准备
|
|
76
|
+
|
|
77
|
+
开始对话前,可通过 `TXIoTAITalkSession.Params` 指定使用的智能体及自定义提示词变量,可通过 `List<TXIoTAITalkSession.Contact>`注册联系人让智能体根据语义触发联系人呼叫,最多可注册 `10` 位联系人。
|
|
78
|
+
``` java
|
|
79
|
+
TXIoTAITalkSession.Params params = new TXIoTAITalkSession.Params();
|
|
80
|
+
params.botId = ""; // 为空则使用平台默认智能体
|
|
81
|
+
params.promptVariablesJson = ""; // 自定义提示词变量,可为空
|
|
82
|
+
|
|
83
|
+
// 可选:注册联系人,供智能体语义触发呼叫,最多可注册 10 位联系人
|
|
84
|
+
List<TXIoTAITalkSession.Contact> contacts = new ArrayList<>();
|
|
85
|
+
TXIoTAITalkSession.Contact contact = new TXIoTAITalkSession.Contact();
|
|
86
|
+
contact.userId = "USER_ID";
|
|
87
|
+
contact.userName = "USER_NAME";
|
|
88
|
+
contacts.add(contact);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 步骤 3:开始对话
|
|
92
|
+
|
|
93
|
+
调用 `startSpeak` 开始 AI 语音对话,该接口为异步调用,开始成功后即可进入语音交互。相关接口如下:
|
|
94
|
+
``` java
|
|
95
|
+
aiTalkSession.startSpeak(params, contacts, new TXIoTCallback() {
|
|
96
|
+
@Override
|
|
97
|
+
public void onSuccess() {
|
|
98
|
+
// 对话已就绪,可开始语音交互
|
|
99
|
+
Log.i(TAG, "startSpeak success");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Override
|
|
103
|
+
public void onError(int code, String desc) {
|
|
104
|
+
Log.e(TAG, "startSpeak failed: " + code + ", " + desc);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 步骤 4:发送文本与打断(可选)
|
|
110
|
+
|
|
111
|
+
对话过程中,除语音交互外,还可通过以下接口进行文本输入与打断控制。以下接口返回 `0`(`TXIoTError.SUCCESS`)表示调用成功。
|
|
112
|
+
``` java
|
|
113
|
+
// 发送文本消息
|
|
114
|
+
int ret = aiTalkSession.sendText("今天天气怎么样?");
|
|
115
|
+
if (ret != TXIoTError.SUCCESS) {
|
|
116
|
+
Log.e(TAG, "sendText failed: " + ret);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 打断智能体当前播报
|
|
120
|
+
aiTalkSession.interrupt();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### 步骤 5:停止对话
|
|
124
|
+
|
|
125
|
+
结束对话时,调用 `stopSpeak` 停止 AI 语音对话,并在页面销毁时移除监听器释放资源:
|
|
126
|
+
``` java
|
|
127
|
+
aiTalkSession.stopSpeak();
|
|
128
|
+
aiTalkSession.removeListener(listener);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 示例代码
|
|
132
|
+
``` java
|
|
133
|
+
package com.example.iotdemo;
|
|
134
|
+
|
|
135
|
+
import android.os.Bundle;
|
|
136
|
+
import android.util.Log;
|
|
137
|
+
import androidx.annotation.Nullable;
|
|
138
|
+
import androidx.appcompat.app.AppCompatActivity;
|
|
139
|
+
|
|
140
|
+
import com.tencent.liteav.iot.TXIoTAITalkSession;
|
|
141
|
+
import com.tencent.liteav.iot.TXIoTCallback;
|
|
142
|
+
import com.tencent.liteav.iot.TXIoTError;
|
|
143
|
+
|
|
144
|
+
import java.util.ArrayList;
|
|
145
|
+
import java.util.List;
|
|
146
|
+
|
|
147
|
+
public class AITalkActivity extends AppCompatActivity {
|
|
148
|
+
|
|
149
|
+
private static final String TAG = "AITalkActivity";
|
|
150
|
+
|
|
151
|
+
private TXIoTAITalkSession mAITalkSession;
|
|
152
|
+
|
|
153
|
+
private final TXIoTAITalkSession.Listener mListener = new TXIoTAITalkSession.Listener() {
|
|
154
|
+
@Override
|
|
155
|
+
public void onReceiveAsrText(String text) {
|
|
156
|
+
Log.i(TAG, "asr text: " + text);
|
|
157
|
+
// 将用户语音识别结果显示到屏幕
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@Override
|
|
161
|
+
public void onReceiveBotText(String text) {
|
|
162
|
+
Log.i(TAG, "bot text: " + text);
|
|
163
|
+
// 将智能体回复文本显示到屏幕
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@Override
|
|
167
|
+
public void onBotStateChanged(TXIoTAITalkSession.BotState oldState,
|
|
168
|
+
TXIoTAITalkSession.BotState newState) {
|
|
169
|
+
Log.i(TAG, "bot state: " + oldState + " -> " + newState);
|
|
170
|
+
// 根据 newState 更新交互状态
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@Override
|
|
174
|
+
public void onLaunchCall(TXIoTAITalkSession.Contact contact) {
|
|
175
|
+
Log.i(TAG, "launch call to: " + contact.userName);
|
|
176
|
+
// 智能体语义触发呼叫,可据此发起音视频通话
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
@Override
|
|
180
|
+
public void onError(int errorCode, String errorMsg) {
|
|
181
|
+
Log.e(TAG, "aitalk error: " + errorCode + ", " + errorMsg);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
@Override
|
|
186
|
+
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
187
|
+
super.onCreate(savedInstanceState);
|
|
188
|
+
setContentView(R.layout.activity_aitalk);
|
|
189
|
+
|
|
190
|
+
// 前提:SDK 已完成初始化并登录成功(参见「登录与注册」)
|
|
191
|
+
mAITalkSession = TXIoTAITalkSession.getInstance();
|
|
192
|
+
mAITalkSession.addListener(mListener);
|
|
193
|
+
|
|
194
|
+
startAITalk();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private void startAITalk() {
|
|
198
|
+
// 组装对话参数
|
|
199
|
+
TXIoTAITalkSession.Params params = new TXIoTAITalkSession.Params();
|
|
200
|
+
params.botId = ""; // 为空则使用平台默认智能体
|
|
201
|
+
params.promptVariablesJson = null;
|
|
202
|
+
|
|
203
|
+
// 可选:注册联系人,供智能体语义触发呼叫
|
|
204
|
+
List<TXIoTAITalkSession.Contact> contacts = new ArrayList<>();
|
|
205
|
+
TXIoTAITalkSession.Contact contact = new TXIoTAITalkSession.Contact();
|
|
206
|
+
contact.userId = "USER_ID";
|
|
207
|
+
contact.userName = "爸爸";
|
|
208
|
+
contacts.add(contact);
|
|
209
|
+
|
|
210
|
+
// 异步开始对话
|
|
211
|
+
mAITalkSession.startSpeak(params, contacts, new TXIoTCallback() {
|
|
212
|
+
@Override
|
|
213
|
+
public void onSuccess() {
|
|
214
|
+
Log.i(TAG, "startSpeak success");
|
|
215
|
+
// 对话已就绪,可开始语音交互;也可直接发送文本
|
|
216
|
+
int ret = mAITalkSession.sendText("你好");
|
|
217
|
+
if (ret != TXIoTError.SUCCESS) {
|
|
218
|
+
Log.e(TAG, "sendText failed: " + ret);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@Override
|
|
223
|
+
public void onError(int code, String desc) {
|
|
224
|
+
Log.e(TAG, "startSpeak failed: " + code + ", " + desc);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
@Override
|
|
230
|
+
protected void onDestroy() {
|
|
231
|
+
// 释放资源
|
|
232
|
+
mAITalkSession.stopSpeak();
|
|
233
|
+
mAITalkSession.removeListener(mListener);
|
|
234
|
+
super.onDestroy();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## 常见问题
|
|
240
|
+
|
|
241
|
+
|现象|排查建议|
|
|
242
|
+
|---------|---------|
|
|
243
|
+
|启动 AI 对话返回 `AITALK_NOT_ACTIVATED`(-204)|设备的 TWeTalk license 未激活,请确认已开通服务并完成设备 license 激活。|
|
|
244
|
+
|未收到机器人音频|确认 `startSpeak` 是否回调成功。|
|
|
245
|
+
|收不到 `onLaunchCall` 回调|确认在 `startSpeak` 时传入了有效的联系人列表;确认用户语音中提及的联系人名称与注册的 `userName` 能够匹配。|
|
|
246
|
+
|接口调用返回 `NOT_LOGGED_IN`(-15)|AI 对话相关接口必须在登录成功后调用,请确认设备已成功登录(参见 登录与注册)。|
|
|
247
|
+
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
---
|
|
2
|
+
product: iot
|
|
3
|
+
framework:
|
|
4
|
+
- linux
|
|
5
|
+
types:
|
|
6
|
+
- feature
|
|
7
|
+
retrievable_scope: always
|
|
8
|
+
feature_domain: device
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
本文将介绍 Linux/RTOS 设备如何基于 C SDK 接入 AI 对话服务。
|
|
12
|
+
|
|
13
|
+
## 前提条件
|
|
14
|
+
1. 您需要先 [注册腾讯云](https://cloud.tencent.com/document/product/378/17985) 账号,并完成 [实名认证](https://cloud.tencent.com/document/product/378/3629)。
|
|
15
|
+
|
|
16
|
+
2. 购买了 [腾讯云物联网开发平台](https://buy.cloud.tencent.com/iotexplorer) 相关实例,开通并激活了 智能语音增值服务。
|
|
17
|
+
|
|
18
|
+
3. 在 [物联网开发平台控制台](https://console.cloud.tencent.com/iotexplorer/v2) 拿到了本 Demo 所需要的设备三元组(**已经激活过了**)。
|
|
19
|
+
|
|
20
|
+
4. 已按 登录与注册 文档跑通 `tc_iot_login` 登录。
|
|
21
|
+
|
|
22
|
+
> **警告:**
|
|
23
|
+
>
|
|
24
|
+
> AI 对话服务属于增值服务,必须开通此增值服务后激活设备 license 才可以正常使用。
|
|
25
|
+
>
|
|
26
|
+
|
|
27
|
+
## 接入步骤
|
|
28
|
+
|
|
29
|
+
### 步骤 1:初始化 AI 对话模块
|
|
30
|
+
|
|
31
|
+
请从 下载页面 获取包含 `talk` 模块的 SDK 包。SDK 包名中 `talk` 表示 AI 对话功能,例如:`tc_iot_sdk_linux_x64_c_av_cs_talk.zip`。
|
|
32
|
+
|
|
33
|
+
登录 成功后,调用 `tc_iot_aitalk_init` 初始化 AI 对话模块并注册回调。常用回调如下:
|
|
34
|
+
|
|
35
|
+
|回调|说明|
|
|
36
|
+
|---------|---------|
|
|
37
|
+
|`on_receive_bot_audio`|接收机器人回复的音频帧,将数据送入设备扬声器播放。|
|
|
38
|
+
|`on_receive_bot_text`|接收机器人回复的文本,可用于屏幕或日志展示。|
|
|
39
|
+
|`on_receive_asr_text`|接收用户语音识别结果。|
|
|
40
|
+
|`on_bot_state_changed`|机器人状态变化通知,例如监听、思考、播报和结束。|
|
|
41
|
+
|`on_launch_call`|机器人触发联系人呼叫时回调,需要先注册联系人。|
|
|
42
|
+
|`on_error`|AI 对话过程中发生错误时回调。|
|
|
43
|
+
|
|
44
|
+
``` c
|
|
45
|
+
#include <stdio.h>
|
|
46
|
+
#include <string.h>
|
|
47
|
+
#include "audio_device.h"
|
|
48
|
+
#include "tc_iot_aitalk.h"
|
|
49
|
+
|
|
50
|
+
static void on_bot_audio(const tc_iot_audio_frame *frame, void *user_data) {
|
|
51
|
+
(void)user_data;
|
|
52
|
+
if (!frame || !frame->data || frame->data_size == 0) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (audio_device_write(frame->data, frame->data_size, frame->codec) != 0) {
|
|
56
|
+
printf("play bot audio failed\n");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void on_aitalk_error(tc_iot_error_e code, const char *message, void *user_data) {
|
|
61
|
+
(void)user_data;
|
|
62
|
+
printf("aitalk error: %d, %s\n", code, message ? message : "");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static tc_iot_error_e init_aitalk(void) {
|
|
66
|
+
tc_iot_aitalk_observer_s observer = {0};
|
|
67
|
+
observer.on_receive_bot_audio = on_bot_audio;
|
|
68
|
+
observer.on_error = on_aitalk_error;
|
|
69
|
+
|
|
70
|
+
tc_iot_aitalk_init_params_s params = {0};
|
|
71
|
+
params.audio_option.codec = TC_IOT_AUDIO_CODEC_OPUS; // 音频编码传输格式,可选 aac/opus/g722
|
|
72
|
+
params.audio_option.sample_rate = TC_IOT_AUDIO_SAMPLE_RATE_16000; // 音频采样率默认 16 kHz
|
|
73
|
+
params.audio_option.frame_duration_ms = 20; // g722/opus 默认填 20 ms,aac 默认填 64 ms
|
|
74
|
+
/* bot_id 为空时使用平台默认智能体配置; prompt_variables_json 是用户自定义信息 */
|
|
75
|
+
|
|
76
|
+
return tc_iot_aitalk_init(¶ms, &observer, NULL);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
> **注意:**
|
|
81
|
+
> `tc_iot_aitalk_init` 必须在 `tc_iot_login` 登录成功后调用。音频的编码、采样率和帧长应与设备音频采集链路保持一致。
|
|
82
|
+
>
|
|
83
|
+
|
|
84
|
+
### 步骤 2:注册联系人(可选)
|
|
85
|
+
|
|
86
|
+
如需让机器人根据语义触发联系人呼叫,请在开始对话前调用 `tc_iot_aitalk_register_contacts`。最多可注册 `10` 位联系人。
|
|
87
|
+
``` c
|
|
88
|
+
static void on_register_contacts(tc_iot_error_e code, const char *message) {
|
|
89
|
+
printf("register contacts: %d, %s\n", code, message ? message : "");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static tc_iot_contact_s contacts[] = {
|
|
93
|
+
{.user_id = "USER_ID", .user_name = "联系人名称"},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
tc_iot_aitalk_register_contacts(contacts, 1, on_register_contacts);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 步骤 3:开始对话并发送音频
|
|
100
|
+
|
|
101
|
+
调用 `tc_iot_aitalk_start_speak` 开始对话。接口为异步调用,请在回调返回成功后再启动麦克风采集。开始采集前,请先完成平台音频设备初始化(示例中的 `audio_device_*` 为平台适配接口)。
|
|
102
|
+
|
|
103
|
+
|模式|说明|
|
|
104
|
+
|---------|---------|
|
|
105
|
+
|`TC_IOT_AITALK_MODE_CONTINUOUS`|持续对话模式,适用于免按键交互场景。|
|
|
106
|
+
|`TC_IOT_AITALK_MODE_PUSH_TO_TALK`|按键说话模式,适用于按住按键采集语音的场景(**暂不支持**)。|
|
|
107
|
+
|
|
108
|
+
``` c
|
|
109
|
+
static void on_mic_captured(const uint8_t *data, uint32_t size,
|
|
110
|
+
tc_iot_audio_codec_e codec, uint64_t pts_ms,
|
|
111
|
+
void *user_data);
|
|
112
|
+
|
|
113
|
+
static void on_start_speak(tc_iot_error_e code, const char *message) {
|
|
114
|
+
if (code != TC_IOT_ERR_SUCCESS) {
|
|
115
|
+
printf("start speak failed: %d, %s\n", code, message ? message : "");
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (audio_device_start_capture(on_mic_captured, NULL) != 0) {
|
|
119
|
+
printf("start capture failed\n");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
tc_iot_aitalk_start_speak(TC_IOT_AITALK_MODE_CONTINUOUS, on_start_speak);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
采集到音频后,组装 `tc_iot_audio_frame` 并调用 `tc_iot_aitalk_send_audio` 发送:
|
|
127
|
+
``` c
|
|
128
|
+
static void on_mic_captured(const uint8_t *data, uint32_t size,
|
|
129
|
+
tc_iot_audio_codec_e codec, uint64_t pts_ms,
|
|
130
|
+
void *user_data) {
|
|
131
|
+
(void)user_data;
|
|
132
|
+
if (!data || size == 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
tc_iot_audio_frame frame = {0};
|
|
137
|
+
frame.codec = codec; // 可直接推送 PCM,由 SDK 内部完成编码与发送
|
|
138
|
+
frame.sample_rate = TC_IOT_AUDIO_SAMPLE_RATE_16000;
|
|
139
|
+
frame.channels = TC_IOT_AUDIO_CHANNEL_MONO;
|
|
140
|
+
frame.frame_duration_ms = 20; // 根据实际情况填写
|
|
141
|
+
frame.data = (uint8_t *)data;
|
|
142
|
+
frame.data_size = size;
|
|
143
|
+
frame.pts_ms = pts_ms;
|
|
144
|
+
|
|
145
|
+
tc_iot_error_e rc = tc_iot_aitalk_send_audio(&frame);
|
|
146
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
147
|
+
printf("send audio failed: %d\n", rc);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
如需直接发送文本,可调用 `tc_iot_aitalk_send_text`。
|
|
153
|
+
|
|
154
|
+
### 步骤 4:中断和停止对话
|
|
155
|
+
|
|
156
|
+
机器人播报过程中,可调用 `tc_iot_aitalk_interrupt` 中断当前回复。结束对话时,先停止本地采集,再停止 AI 对话并释放模块资源。
|
|
157
|
+
``` c
|
|
158
|
+
audio_device_stop_capture(); // 停止音频采集
|
|
159
|
+
tc_iot_aitalk_stop_speak(); // 停止 AI 对话
|
|
160
|
+
tc_iot_aitalk_deinit(); // 反初始化并释放资源,完全退出 AI 对话服务
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
随后请按照 登录与注册 的流程退出登录并反初始化 SDK,则 SDK 完全释放资源。
|
|
164
|
+
|
|
165
|
+
## 完整示例代码
|
|
166
|
+
|
|
167
|
+
以下示例保留 SDK 的真实 API 和关键参数校验;**音频采集、播放、休眠**等平台相关逻辑均以注释标出,请替换为芯片或操作系统提供的接口。
|
|
168
|
+
``` c
|
|
169
|
+
#include <stdbool.h>
|
|
170
|
+
#include <stdint.h>
|
|
171
|
+
#include <stdio.h>
|
|
172
|
+
|
|
173
|
+
#include "tc_iot.h"
|
|
174
|
+
#include "tc_iot_aitalk.h"
|
|
175
|
+
|
|
176
|
+
/* 登录和开始对话均为异步操作,回调中记录结果,主流程据此继续或退出。 */
|
|
177
|
+
static volatile bool s_login_done = false;
|
|
178
|
+
static volatile bool s_start_done = false;
|
|
179
|
+
static volatile bool s_aitalk_error = false;
|
|
180
|
+
static tc_iot_error_e s_login_result = TC_IOT_ERR_SUCCESS;
|
|
181
|
+
static tc_iot_error_e s_start_result = TC_IOT_ERR_SUCCESS;
|
|
182
|
+
|
|
183
|
+
static void on_login(tc_iot_error_e error_code, const char *error_message) {
|
|
184
|
+
s_login_result = error_code;
|
|
185
|
+
s_login_done = true;
|
|
186
|
+
// 登录失败时,可在这里记录 error_message 并通知上层业务。
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static void on_start_speak(tc_iot_error_e error_code, const char *error_message) {
|
|
190
|
+
s_start_result = error_code;
|
|
191
|
+
s_start_done = true;
|
|
192
|
+
// 开始对话失败时,可在这里记录 error_message 并通知上层业务。
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static void on_receive_bot_audio(const tc_iot_audio_frame *frame, void *user_data) {
|
|
196
|
+
(void)user_data;
|
|
197
|
+
if (!frame || !frame->data || frame->data_size == 0) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
// 这里收到的音频是经过解码后的 PCM 音频
|
|
201
|
+
// 将 frame->data 推送到具体的音频设备播放。
|
|
202
|
+
// 例如:调用芯片平台的扬声器播放、PCM 输出或音频框架接口。
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static void on_receive_bot_text(const char *text, void *user_data) {
|
|
206
|
+
(void)user_data;
|
|
207
|
+
if (!text) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
// 将机器人回复文本显示到屏幕,或上传到您的业务服务。
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static void on_receive_asr_text(const char *text, void *user_data) {
|
|
214
|
+
(void)user_data;
|
|
215
|
+
if (!text) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
// 将用户语音识别结果显示到屏幕,或用于本地业务处理。
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static void on_bot_state_changed(tc_iot_aitalk_bot_state_e old_state,
|
|
222
|
+
tc_iot_aitalk_bot_state_e new_state,
|
|
223
|
+
void *user_data) {
|
|
224
|
+
(void)old_state;
|
|
225
|
+
(void)user_data;
|
|
226
|
+
// 根据 new_state 更新设备指示灯、屏幕或其他交互状态。
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static void on_aitalk_error(tc_iot_error_e error_code, const char *error_message,
|
|
230
|
+
void *user_data) {
|
|
231
|
+
(void)user_data;
|
|
232
|
+
s_aitalk_error = true;
|
|
233
|
+
// 记录 error_code 和 error_message,并停止或重试业务流程。
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* 在平台麦克风的数据回调中调用本函数。
|
|
238
|
+
* data、size、codec 和 pts_ms 均来自当前平台的音频采集结果。
|
|
239
|
+
*/
|
|
240
|
+
static void on_mic_captured(const uint8_t *data, uint32_t size,
|
|
241
|
+
tc_iot_audio_codec_e codec, uint64_t pts_ms,
|
|
242
|
+
void *user_data) {
|
|
243
|
+
(void)user_data;
|
|
244
|
+
if (!data || size == 0) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
tc_iot_audio_frame frame = {0};
|
|
249
|
+
frame.codec = codec;
|
|
250
|
+
frame.sample_rate = TC_IOT_AUDIO_SAMPLE_RATE_16000;
|
|
251
|
+
frame.channels = TC_IOT_AUDIO_CHANNEL_MONO;
|
|
252
|
+
frame.frame_duration_ms = 20;
|
|
253
|
+
frame.data = (uint8_t *)data;
|
|
254
|
+
frame.data_size = size;
|
|
255
|
+
frame.pts_ms = pts_ms;
|
|
256
|
+
|
|
257
|
+
tc_iot_error_e rc = tc_iot_aitalk_send_audio(&frame);
|
|
258
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
259
|
+
// 记录发送失败原因;必要时停止采集或触发重连。
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
int main(void) {
|
|
264
|
+
bool sdk_inited = false;
|
|
265
|
+
bool aitalk_inited = false;
|
|
266
|
+
bool speak_started = false;
|
|
267
|
+
bool conversation_completed = false;
|
|
268
|
+
tc_iot_error_e rc;
|
|
269
|
+
|
|
270
|
+
// 1. 配置并初始化 SDK。
|
|
271
|
+
tc_iot_config_s sdk_config = {0};
|
|
272
|
+
sdk_config.storage_path = "./";
|
|
273
|
+
sdk_config.log_level = TC_IOT_LOG_LEVEL_INFO;
|
|
274
|
+
rc = tc_iot_init(&sdk_config);
|
|
275
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
276
|
+
goto cleanup;
|
|
277
|
+
}
|
|
278
|
+
sdk_inited = true;
|
|
279
|
+
|
|
280
|
+
// 2. 从安全配置中读取设备三元组并登录平台。
|
|
281
|
+
tc_iot_device_info_s device_info = {0};
|
|
282
|
+
device_info.product_id = get_product_id();
|
|
283
|
+
device_info.device_id = get_device_name();
|
|
284
|
+
device_info.device_secret = get_device_secret();
|
|
285
|
+
|
|
286
|
+
s_login_done = false;
|
|
287
|
+
rc = tc_iot_login(&device_info, on_login);
|
|
288
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
289
|
+
goto cleanup;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 等待 on_login 回调或超时;休眠接口请替换为具体平台实现。
|
|
293
|
+
wait_until(&s_login_done, 30000);
|
|
294
|
+
if (!s_login_done || s_login_result != TC_IOT_ERR_SUCCESS) {
|
|
295
|
+
goto cleanup;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 3. 注册 AI 对话事件回调并初始化模块。
|
|
299
|
+
tc_iot_aitalk_observer_s observer = {0};
|
|
300
|
+
observer.on_receive_bot_audio = on_receive_bot_audio;
|
|
301
|
+
observer.on_receive_bot_text = on_receive_bot_text;
|
|
302
|
+
observer.on_receive_asr_text = on_receive_asr_text;
|
|
303
|
+
observer.on_bot_state_changed = on_bot_state_changed;
|
|
304
|
+
observer.on_error = on_aitalk_error;
|
|
305
|
+
|
|
306
|
+
tc_iot_aitalk_init_params_s init_params = {0};
|
|
307
|
+
// 根据实际采集参数设置编码、采样率和单帧时长。
|
|
308
|
+
init_params.audio_option = get_platform_audio_option();
|
|
309
|
+
rc = tc_iot_aitalk_init(&init_params, &observer, NULL);
|
|
310
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
311
|
+
goto cleanup;
|
|
312
|
+
}
|
|
313
|
+
aitalk_inited = true;
|
|
314
|
+
|
|
315
|
+
// 4. 如需机器人触发联系人呼叫,在开始对话前注册联系人。
|
|
316
|
+
// tc_iot_aitalk_register_contacts(contacts, contact_count, on_register_contacts);
|
|
317
|
+
|
|
318
|
+
// 5. 初始化具体平台的麦克风和扬声器。
|
|
319
|
+
// 麦克风采集到一帧音频后,调用 on_mic_captured() 发送给 AI 对话服务。
|
|
320
|
+
|
|
321
|
+
// 6. 异步开始持续对话,成功后再启动麦克风采集。
|
|
322
|
+
s_start_done = false;
|
|
323
|
+
rc = tc_iot_aitalk_start_speak(TC_IOT_AITALK_MODE_CONTINUOUS, on_start_speak);
|
|
324
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
325
|
+
goto cleanup;
|
|
326
|
+
}
|
|
327
|
+
wait_until(&s_start_done, 30000);
|
|
328
|
+
if (!s_start_done || s_start_result != TC_IOT_ERR_SUCCESS) {
|
|
329
|
+
goto cleanup;
|
|
330
|
+
}
|
|
331
|
+
speak_started = true;
|
|
332
|
+
|
|
333
|
+
// 启动具体平台的麦克风采集流程。
|
|
334
|
+
// 采集线程或回调中不要阻塞过久,按帧调用 on_mic_captured()。
|
|
335
|
+
|
|
336
|
+
while (!s_aitalk_error && !is_exit_requested()) {
|
|
337
|
+
// 使用具体平台的休眠、事件循环或任务调度机制。
|
|
338
|
+
}
|
|
339
|
+
conversation_completed = !s_aitalk_error;
|
|
340
|
+
|
|
341
|
+
cleanup:
|
|
342
|
+
// 先停止具体平台的麦克风采集和扬声器播放。
|
|
343
|
+
if (speak_started) {
|
|
344
|
+
tc_iot_aitalk_stop_speak();
|
|
345
|
+
}
|
|
346
|
+
if (aitalk_inited) {
|
|
347
|
+
tc_iot_aitalk_deinit();
|
|
348
|
+
}
|
|
349
|
+
if (sdk_inited) {
|
|
350
|
+
tc_iot_deinit();
|
|
351
|
+
}
|
|
352
|
+
return conversation_completed ? 0 : 1;
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
> **注意:**
|
|
357
|
+
> 该示例使用持续对话模式。若需联系人呼叫,请在 `tc_iot_aitalk_start_speak` 前按 步骤 2 的方式注册联系人。
|
|
358
|
+
>
|
|
359
|
+
|
|
360
|
+
## 常见问题
|
|
361
|
+
|
|
362
|
+
|现象|排查建议|
|
|
363
|
+
|---------|---------|
|
|
364
|
+
|启动 AI 对话返回 `-204`(License 未激活)|设备的 TWeTalk license 未激活,请确认已开通服务并完成设备 license 激活。|
|
|
365
|
+
|未收到机器人音频|确认 `tc_iot_aitalk_start_speak` 的回调已成功返回,且麦克风采集后持续调用了 `tc_iot_aitalk_send_audio`。|
|
|
366
|
+
|收到音频但没有声音|检查 `on_receive_bot_audio` 是否已将数据送入平台播放接口,并确认播放设备支持回调中的音频编码。|
|
|
367
|
+
|未触发联系人呼叫|确认在 `tc_iot_aitalk_start_speak` 前调用了 `tc_iot_aitalk_register_contacts`,且联系人信息有效。|
|
|
368
|
+
|