实时数字人交互SDK 集成开发
1. 简介
实时数字人交互SDK 提供了一套简单易用的接口,让开发者可以在各种平台上快速集成数字人交互功能。通过该 SDK,您可以在 Web、iOS、Android 和小程序中实现与数字人的实时交互。
1.1 功能特点
- 多平台支持:同时支持 Web、iOS、Android 和小程序
- 悬浮按钮入口:提供悬浮按钮,点击即可打开数字人对话界面
- 自定义加载界面:支持自定义加载界面,提升用户体验
- 热词过滤:支持热词过滤,保障交互内容的安全性
- 简单易用:提供简洁的 API,快速集成到现有项目
1.2 支持的平台
- Web 端(浏览器)
- iOS 端(Swift)
- Android 端(Kotlin/Java)
- 小程序端(微信小程序)
2. 快速开始
2.1 获取 API Key
在开始之前,您需要在 NuwaAI平台完成以下步骤:
- 注册一个 NuwaAI账号,并登录
- 登录后,记录下用户ID
- 创建一个数字人,并记录下数字人 ID
- 创建一个API Key,并记录下 API Key
2.2 安装 SDK
根据您的平台选择相应的安装方式:
示例
# 使用 npm 安装
npm install nuwaai-human-sdk
# 或直接下载 SDK 包
# 下载地址:https://nuwaai.com/demo/npcTalk/npcTalk.tgz
2.3 第一个数字人交互
现在,让我们创建第一个数字人交互应用:
示例
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SDK 初始化页面</title>
<link rel="stylesheet" href="/node_modules/nuwaai-human-sdk/css/demo.css">
<script type="module" src="/node_modules/nuwaai-human-sdk/js/npcTalkFloatingButton.js"></script>
</head>
<body>
<div class="container">
<h1>🔧 SDK 初始化配置</h1>
<form id="sdkForm">
<div class="form-group">
<label for="apiKey">API Key</label>
<input type="text" id="apiKey" name="apiKey" placeholder="请输入您的 API Key" value="sk-jqZ-gBPcRCG1nLpaenmS1gew7rcCkIs-NUWSPzpd3DeWYxobM_CHm-uPTCa8gDPv">
</div>
<div class="form-group">
<label for="avatarId">数字人 ID</label>
<input type="text" id="avatarId" name="avatarId" placeholder="请输入数字人 ID" value="2027224426241638401">
</div>
<div class="form-group">
<label for="userId">用户 ID</label>
<input type="text" id="userId" name="userId" placeholder="请输入用户 ID" value="54">
</div>
<button type="button" class="btn-init" onclick="initializeSDK()">初始化 SDK</button>
</form>
<div id="result" class="result"></div>
</div>
<script>
// 初始化 SDK 函数
function initializeSDK() {
let floatingButtonInstance = null;
const apiKey = document.getElementById('apiKey').value.trim();
const avatarId = document.getElementById('avatarId').value.trim();
const userId = document.getElementById('userId').value.trim();
const resultDiv = document.getElementById('result');
// 验证输入
if (!apiKey || !avatarId || !userId) {
resultDiv.className = 'result error';
resultDiv.textContent = '请填写所有必填字段';
return;
}
fetch('https://api.nuwaai.com/web/apiKey/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
secretKey: apiKey,
}),
})
.then(res => res.json())
.then(data => {
if (data.code === 0) {
console.log('token:', data.data);
localStorage.setItem('token', data.data);
initWithCustomLoading(avatarId, userId);
} else {
console.error('请求失败:', data.msg);
}
})
.catch(error => {
console.error('请求出错:', error);
});
function initWithCustomLoading(avatorId, userId) {
try {
// 销毁之前的实例
if (floatingButtonInstance) {
floatingButtonInstance.destroy();
}
// 创建新实例
// loadingConfig 可以直接传递对象,npcTalkFloatingButton.js 会自动处理编码
floatingButtonInstance = new window.NpcTalkFloatingButton({
avatorid: avatorId,
userid: userId,
npcPage1: '/node_modules/nuwaai-human-sdk/index-sdk.html',
loadingConfig: null,
disabledButton: false, //按钮隐藏 调用floatingButtonInstance.navigateToNpcTalk()
title: '数字人聊天', //页面标题(可选,会自动更新 index-sdk.html 的 <title>)
href: '', //页面logo地址(可选,会自动更新 index-sdk.html 的 <link rel="icon">)
sliceTime:10000, //毫秒
backAddress:'', // 返回地址
hotWords:['咱们','休息一下','你好呀'] //热词,用来过滤敏感词
});
} catch (error) {
console.error('初始化失败:', error);
}
}
}
// 监听回车键提交
document.getElementById('sdkForm').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
initializeSDK();
}
});
</script>
</body>
</html>
至此,您已经成功地将 Nuwa 数字人实时交互 SDK 集成到了您的应用中。
web端集成后演示:点击这里
3. SDK开发详解
3.1 获取 Access Token
在使用 SDK 前,需要先获取 Access Token: 获取 Access Token
3.2 初始化 SDK
示例
let floatingButtonInstance = new window.NpcTalkFloatingButton({
avatorid: '2027224426241638401',
userid: '54',
npcPage1: 'index-sdk.html',
loadingConfig: {
html: '<div class="loading">加载中...</div>',
style: {
background: '#fff',
color: '#333'
}
},
disabledButton: false,
title: '数字人聊天',
href: '',
sliceTime: 10000,
backAddress: '',
hotWord: ['咱们', '休息一下', '你好呀']
});
| 参数 | 类型 | 必填 | 说明 | 平台 |
|---|---|---|---|---|
| avatorid | String | 是 | 数字人 ID | 全部 |
| userid | String | 是 | 用户 ID | 全部 |
| token | String | 是 | Access Token | iOS/Android/小程序 |
| npcPage1 | String | 是 | 数字人聊天页面路径 | Web/小程序 |
| loadingConfig | Object | 否 | 自定义加载界面配置 | 全部 |
| disabledButton | Boolean | 否 | 是否隐藏悬浮按钮 | 全部 |
| title | String | 否 | 页面标题 | Web/iOS/Android |
| href | String | 否 | 页面 logo 地址 | Web |
| sliceTime | Number | 否 | 时间切片,单位毫秒 | Web |
| backAddress | String | 否 | 返回地址 | Web |
| hotWord | Array | 否 | 热词列表,用于过滤敏感词 | 全部 |
3.3 自定义加载界面
您可以自定义加载界面,提升用户体验:
示例
const loadingConfig = {
html: `
<div class="custom-loading-wrapper">
<div class="custom-loading-spinner"></div>
<div class="custom-loading-text">正在连接数字人...</div>
<div class="custom-loading-subtext">请稍候</div>
</div>
`,
style: {
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
},
additionalCSS: `
.custom-loading-container .custom-loading-wrapper {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.custom-loading-container .custom-loading-spinner {
width: 60px;
height: 60px;
border: 4px solid rgba(255, 255, 255, 0.3);
border-top-color: white;
border-radius: 50%;
animation: custom-loading-spin 1s linear infinite;
margin-bottom: 20px;
}
@keyframes custom-loading-spin {
to { transform: rotate(360deg); }
}
`,
};
floatingButtonInstance = new window.NpcTalkFloatingButton({
// 其他参数...
loadingConfig: loadingConfig
});
3.4 控制悬浮按钮
示例
// 隐藏悬浮按钮
floatingButtonInstance = new window.NpcTalkFloatingButton({
// 其他参数...
disabledButton: true
});
// 手动打开数字人聊天界面
floatingButtonInstance.navigateToNpcTalk();
// 销毁 SDK 实例
floatingButtonInstance.destroy();
| 方法 | 说明 | 平台 |
|---|---|---|
| showFloatingButton() | 显示悬浮按钮 | 全部 |
| hideFloatingButton() | 隐藏悬浮按钮 | 全部 |
| navigateToNpcTalk() | 打开数字人聊天界面 | 全部 |
4. 注意事项
4.1 Web 平台
要求
- 设备需要具备麦克风
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
权限
- 具有麦克风及风权限
4.2 iOS 平台
要求
- iOS 12.0+
- Xcode 12.0+
- Swift 5.0+
权限
- 在
Info.plist中添加麦克风权限:
<key>NSMicrophoneUsageDescription</key>
<string>需要使用麦克风进行语音通话</string>
集成步骤
安装 NuwaSDK:
- 使用 CocoaPods:在 Podfile 中添加
pod 'NuwaSDK' - 或直接将 NuwaSDK.framework 添加到项目中
- 使用 CocoaPods:在 Podfile 中添加
导入头文件:
import NuwaSDK创建配置:
let config = NuwaFloatingButtonConfig() config.apiKey = "your_api_key" config.avatorId = "your_avatar_id" config.userId = "your_user_id"创建并显示悬浮按钮:
let floatingButton = NuwaFloatingButton(config: config) floatingButton.show(in: view)隐藏悬浮按钮(可选):
floatingButton.hide()
4.3 Android 平台
要求
- Android 5.0+
- minSdkVersion 21+
- targetSdkVersion 33+
权限
在 AndroidManifest.xml 中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
4.4 小程序平台
要求
- 微信小程序基础库 2.10.0+
- 已开通实时音视频功能
配置
在 app.json 中添加:
{
"requiredBackgroundModes": ["audio", "video"],
"permission": {
"scope.camera": {
"desc": "用于视频通话"
},
"scope.record": {
"desc": "用于语音通话"
}
}
}
5. 常见问题
5.1 数字人聊天界面不显示
问题:点击悬浮按钮后,数字人聊天界面不显示。
解决方案:
- 检查
npcPage1路径是否正确 - 确保相关页面存在且可访问
- 查看浏览器的控制台或日志中的错误信息
5.2 热词过滤不生效
问题:设置的热词过滤没有生效。
解决方案:
- 确保
hotWord参数是一个数组 - 检查热词是否正确设置
- 确保热词格式符合要求
5.3 平台特定问题
iOS
- 问题:悬浮按钮不显示 解决方案:检查 API Key、数字人 ID 是否正确,确保网络连接正常
- 问题:视频不显示 解决方案:确保已添加摄像头权限,检查设备摄像头是否可用
- 问题:音频不工作 解决方案:确保已添加麦克风权限,检查设备音量是否开启
Android
- 问题:权限被拒绝 解决方案:在运行时请求麦克风权限
小程序
- 问题:无法打开麦克风 解决方案:确保用户已授权麦克风权限