语音交互服务 SIS-语音合成(webSocket版):代码示例
时间:2024-12-04 10:03:55
代码示例
如下示例仅供参考,最新代码请前往SDK(websocket)章节获取并运行。
/* * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. */ package com.huaweicloud.sis.android.demo.tts; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.cloud.sdk.util.StringUtils; import com.huaweicloud.sdk.core.utils.JsonUtils; import com.huaweicloud.sis.android.demo.R; import com.huaweicloud.sis.android.demo.service.AudioTrackService; import com.huaweicloud.sis.android.demo.Config; import com.huaweicloud.sis.android.demo.service.AudioTrackServiceCallback; import sis.android.sdk.RttsClient; import sis.android.sdk.bean.AuthInfo; import sis.android.sdk.bean.SisHttpConfig; import sis.android.sdk.bean.request.RttsRequest; import sis.android.sdk.bean.response.RttsResponse; import sis.android.sdk.exception.SisException; import sis.android.sdk.listeners.RttsResponseListener; /** * 功能描述 * 语音合成 websocket * * @since 2022-07-18 */ public class RttsActivity extends AppCompatActivity { private EditText textView; private Button startPlay; private RttsClient rttsClient; private SisHttpConfig sisHttpConfig; private AudioTrackService audioTrackService; private AudioTrackServiceCallback audioPlayerCallback; private AuthInfo authInfo; private static int dataAcceptanceTime = 1000 * 60 * 5; private RttsResponseListener rttsResponseListener = new RttsResponseListener() { @Override public void onTranscriptionConnect() { Log.i("info", "建立连接后回调"); } @Override public void onTranscriptionClose() { Log.i("info", "关闭连接后回调"); } @Override public void onTranscriptionFail(RttsResponse response) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "出现异常" + JsonUtils.toJSON(response), Toast.LENGTH_SHORT).show(); } }); updateButtonState(startPlay, true); Log.i("info", "长连接失败后回调" + JsonUtils.toJSON(response)); } @Override public void onTranscriptionBegin(RttsResponse response) { Log.i("info", "开始合成时的响应事件" + response.toString()); } @Override public void onTranscriptionEnd(RttsResponse response) { rttsClient.close(); audioTrackService.setPlayState(AudioTrackService.PlayState.playEnd); } @Override public void onTranscriptionError(RttsResponse response) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "出现异常" + JsonUtils.toJSON(response), Toast.LENGTH_SHORT).show(); } }); updateButtonState(startPlay, true); Log.i("info", "合成时发生错误的响应事件" + response.toString()); } @Override public void onTranscriptionResponse(byte[] bytes) { Log.i("info", "合成过程中返回的二进制流"); audioTrackService.setAudioData(bytes); } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rtts); } @Override protected void onStart() { super.onStart(); initView(); initResources(); } @Override protected void onDestroy() { super.onDestroy(); if (audioTrackService != null) { audioTrackService.stop(); audioTrackService.releaseTrack(); } } /** * 初始化UI */ private void initView() { textView = findViewById(R.id.itext); startPlay = findViewById(R.id.startplay); startPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { updateButtonState(startPlay, false); new Thread(new Runnable() { @Override public void run() { try { audioTrackService.play(); rttsClient = new RttsClient(authInfo, sisHttpConfig, rttsResponseListener); RttsRequest rttsRequest = rttsRequets(); rttsClient.connect(); rttsClient.sendData(rttsRequest); } catch (SisException e) { Log.e("error", e.getErrorCode() + e.getErrorMsg()); } } }).start(); } }); } // 用于设置按钮的状态 private void updateButtonState(final Button btn, final boolean state) { runOnUiThread(new Runnable() { @Override public void run() { btn.setEnabled(state); } }); } /** * 初始化资源 */ private void initResources() { authInfo = new AuthInfo(this.getString(R.string.HUAWEICLOUD_SDK_AK), this.getString(R.string.HUAWEICLOUD_SDK_SK), Config.REGION, Config.PROJECT_ID); sisHttpConfig = new SisHttpConfig(); // 设置等待事件为5分钟 sisHttpConfig.setWebsocketWaitTimeout(dataAcceptanceTime); audioPlayerCallback = new AudioTrackServiceCallback() { @Override public void playStart() { } @Override public void playOver() { updateButtonState(startPlay, true); } }; audioTrackService = new AudioTrackService(audioPlayerCallback); } /** * 功能描述 * 语音合成websocket 版 */ private RttsRequest rttsRequets() { RttsRequest rttsRequest = new RttsRequest(); RttsRequest.Config config = new RttsRequest.Config(); config.setAudioFormat("pcm"); config.setProperty("chinese_huaxiaomei_common"); config.setSampleRate("16000"); config.setPitch(0); config.setVolume(50); rttsRequest.setCommand("START"); rttsRequest.setConfig(config); String text = textView.getText().toString().trim(); if (!StringUtils.isNullOrEmpty(text)) { rttsRequest.setText(text); } else { rttsRequest.setText("请输入合成音频"); } return rttsRequest; } }
support.huaweicloud.com/sdkreference-sis/sis_05_0089.html
看了此文的人还看了
CDN加速
GaussDB
文字转换成语音
免费的服务器
如何创建网站
域名网站购买
私有云桌面
云主机哪个好
域名怎么备案
手机云电脑
SSL证书申请
云点播服务器
免费OCR是什么
电脑云桌面
域名备案怎么弄
语音转文字
文字图片识别
云桌面是什么
网址安全检测
网站建设搭建
国外CDN加速
SSL免费证书申请
短信批量发送
图片OCR识别
云数据库MySQL
个人域名购买
录音转文字
扫描图片识别文字
OCR图片识别
行驶证识别
虚拟电话号码
电话呼叫中心软件
怎么制作一个网站
Email注册网站
华为VNC
图像文字识别
企业网站制作
个人网站搭建
华为云计算
免费租用云托管
云桌面云服务器
ocr文字识别免费版
HTTPS证书申请
图片文字识别转换
国外域名注册商
使用免费虚拟主机
云电脑主机多少钱
鲲鹏云手机
短信验证码平台
OCR图片文字识别
SSL证书是什么
申请企业邮箱步骤
免费的企业用邮箱
云免流搭建教程
域名价格
推荐文章