# huaweicloud-metastudio-voicedrive-java
**Repository Path**: HuaweiCloudDeveloper/huaweicloud-metastudio-voicedrive-java
## Basic Information
- **Project Name**: huaweicloud-metastudio-voicedrive-java
- **Description**: 输入文本,MetaStudio通过TTS转成语音后驱动数字人表情和肢体动作的Java示例代码
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master-dev
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2023-04-17
- **Last Updated**: 2025-06-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## 版本说明
本示例基于华为云SDK V3.0版本开发
## 功能介绍
**什么是华为云MetaStudio服务?**
华为云MetaStudio服务联合伙伴打造数字内容生产线,帮助千行百业实现虚拟世界与现实世界无缝融合。数字人生产线是数字内容生产线的主要组成部分,包含数字人生产、数字人视频制作和数字人直播等场景化的应用。
华为云MetaStudio服务在数字内容生产线中为伙伴提供PaaS原子API能力。
MetaStudio提供的PaaS API能力包括:
* 照片建模能力,输入单张照片,输出3D数字人模型。
* 语音驱动能力,输入文本,MetaStudio内部TTS转成语音后,输出驱动数字人的表情基数据和肢体动作数据。
* 视频驱动能力,输入摄像头视频流,输出数字人的表情基数据和肢体动作数据。
**您将学到什么?**
华为云提供了 MetaStudio 服务端 SDK,您可以直接集成服务端 SDK 来调用 MetaStudio 的相关 API,从而实现对 MetaStudio 的快速操作。
该场景示例代码以数字人语音驱动为例,介绍如何使用MetaStudio Java SDK将输入的文本数据转换为驱动数字的表情基系数和肢体动作数据。
语音驱动可用于数字人的视频制作等场景。
## 开发时序图

## 前置条件
- 1、获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
- 2、您需要拥有华为云租户账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 [访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html) 。
- 3、华为云 Java SDK 支持 **Java JDK 1.8** 及其以上版本。
- 4、由于当前MetaStudio服务还处于公测阶段,仅对伙伴开放权限,需要申请才能使用,请提供联系华为销售人员开通MetaStudio服务权限。
具体操作步骤请参考:[华为云MetaStudio开发指南](https://support.huaweicloud.com/api-metastudio/metastudio_02_0000.html) 。
## 接口参数说明
关于语音驱动接口的详细说明可参见:
MetaStudio API参考的[“语音驱动”](https://support.huaweicloud.com/api-metastudio/CreateTTSA.html) 章节。
### SDK获取和安装
开发者可以通过Maven方式获取和安装SDK,包括“huaweicloud-sdk-core”和“huaweicloud-sdk-metastudio”。需要在Java项目的pom.xml文件中加入如下的依赖项。
```xml
com.huaweicloud.sdk
huaweicloud-sdk-core
3.1.36
com.huaweicloud.sdk
huaweicloud-sdk-metastudio
3.1.36
org.slf4j
slf4j-nop
1.7.2
```
## 示例代码
```java
public class MetaStudioVoiceDrive {
private static final Logger logger = LoggerFactory.getLogger(MetaStudioVoiceDrive.class);
public static void main(String[] args) throws InterruptedException {
System.out.println("Start HUAWEI CLOUD MetaStudio Voice Drive Java Demo...");
ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");
// 初始数字人服务的客户端
MetaStudioClient client = getClient(MetaStudioRegion.CN_NORTH_4, auth);
// 获取语音驱动任务列表
ListTTSAJobs(client);
// 创建语音驱动任务
String jobId = createTTSAJob(client);
// 创建任务后需要等待 3秒后 再查询
Thread.sleep(3000);
// 获取语音驱动任务详情
ListTTSAData(client, jobId);
}
/**
* 查询语音驱动任务列表
*
* @param client MetaStudio client
* @return 查询语音驱动任务列表响应
*/
public static ListTtsaJobsResponse ListTTSAJobs(MetaStudioClient client) {
System.out.println("ListTTSAJobs start");
try {
ListTtsaJobsRequest request = new ListTtsaJobsRequest();
ListTtsaJobsResponse response = client.listTtsaJobs(request);
System.out.println("ListTtsaJobsResponse :" + response.toString());
return response;
} catch (ConnectionException | RequestTimeoutException e) {
System.out.println("there is some error, exception:" + e);
}
return null;
}
/**
* 创建语音驱动任务
*
* @param client MetaStudio client
* @return 语音驱动任务ID
*/
public static String createTTSAJob(MetaStudioClient client) {
System.out.println("createTTSAJob start");
// 动作资产查询条件
ListAssetsRequest listMotionAssetsRequest = new ListAssetsRequest()
.withAssetSource(ListAssetsRequest.AssetSourceEnum.ALL)
.withAssetType(DigitalAssetInfo.AssetTypeEnum.ANIMATION.getValue())
.withName("小芸_右手打招呼");
// 音色资产查询条件
ListAssetsRequest listVoiceAssetsRequest = new ListAssetsRequest()
.withAssetSource(ListAssetsRequest.AssetSourceEnum.ALL)
.withAssetType(DigitalAssetInfo.AssetTypeEnum.VOICE_MODEL.getValue())
.withName("亲切女声");
try {
// 查询音色资产
ListAssetsResponse voiceAssetsResponse = client.listAssets(listVoiceAssetsRequest);
String voiceAssetId = voiceAssetsResponse.getAssets().get(0).getAssetId();
System.out.println(voiceAssetId);
// 查询动作资产
ListAssetsResponse motionAssetsResponse = client.listAssets(listMotionAssetsRequest);
String motionAssetId = motionAssetsResponse.getAssets().get(0).getAssetId();
System.out.println(motionAssetId);
//含动作标签的语音文本
String text = String.format("大家好,我是云笙,这个是MetaStudio语音驱动的测试用例。" , motionAssetId);
CreateTTSAReq createTtsaReq = new CreateTTSAReq()
.withVoiceAssetId(voiceAssetId) // 音色ID
.withEmotion("HAPPY") // 情感标签
.withPitch(100) // 音高
.withSpeed(100) // 语速
.withVolume(140) // 音量
.withText(text); //带动作标签的文本
CreateTtsaRequest request = new CreateTtsaRequest()
.withBody(createTtsaReq);
CreateTtsaResponse response = client.createTtsa(request);
System.out.println("createTTSAJob :" + response.toString());
logger.info(response.toString());
return response.getJobId();
} catch (ConnectionException | RequestTimeoutException e) {
System.out.println("there is some error, exception:" + e);
}
return null;
}
/**
* 获取语音驱动数据
*
* @param client MetaStudio client
* @param jobId 语音驱动任务ID
* @return 查询语音驱动任务详情响应
*/
public static ListTtsaDataResponse ListTTSAData(MetaStudioClient client, String jobId) {
System.out.println("ListTTSAData start");
try {
ListTtsaDataRequest request = new ListTtsaDataRequest()
.withJobId(jobId);
ListTtsaDataResponse response = client.listTtsaData(request);
logger.info(response.toString());
System.out.println("ListTTSAData :" + response);
return response;
} catch (ConnectionException | RequestTimeoutException e) {
System.out.println("there is some error, exception:" + e);
}
return null;
}
/**
* 创建鉴权凭证
*
* @param ak 租户帐号对应的 Access Key(AK)
* @param sk 租户帐号对应的 Secret Access Key(SK)
* @return 鉴权凭证
*/
public static ICredential getCredential(String ak, String sk, String projectId) {
return new BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(projectId);
}
/**
* 创建MetaStudio client
*
* @param region region信息
* @param auth 鉴权凭证
* @return MetaStudio client
*/
public static MetaStudioClient getClient(Region region, ICredential auth) {
// 使用默认配置
HttpConfig config = HttpConfig.getDefaultHttpConfig();
config.withIgnoreSSLVerification(true);
// 初始化metaStudio服务的客户端
return MetaStudioClient.newBuilder()
.withHttpConfig(config)
.withCredential(auth)
.withRegion(region) // 选择服务所在区域
.build();
}
}
```
## 参考
更多信息请参考:
[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/MetaStudio/doc?api=CreateTtsa)
[《华为云MetaStudio开发指南》](https://support.huaweicloud.com/api-metastudio/metastudio_02_0000.html)
## 修订记录
| 发布日期 | 文档版本 | 修订说明 |
| :--------: | :------: | :----------: |
| 2023-04-12 | 1.0 | 文档首次发布 |