代码拉取完成,页面将自动刷新
Demo中调用library组件,然后通过五个功能操作展示组件。
1、机器人语音对话界面效果:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:id="$+id:abilityChat"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"
>
<DirectionalLayout
ohos:id="$+id:chatToolBar"
ohos:height="match_content"
ohos:width="match_parent"
ohos:background_element="#eee"
ohos:orientation="horizontal"
>
<Image
ohos:id="$+id:icon"
ohos:height="32vp"
ohos:width="32vp"
ohos:margin="4vp"
ohos:scale_mode="zoom_center"
/>
<Text
ohos:id="$+id:title"
ohos:height="match_content"
ohos:width="0vp"
ohos:margin="8vp"
ohos:text=""
ohos:text_size="20fp"
ohos:weight="1"
/>
</DirectionalLayout>
<Text
ohos:height="2vp"
ohos:width="match_parent"
ohos:background_element="#ddd"
/>
<Text
ohos:id="$+id:yandex"
ohos:height="match_content"
ohos:width="match_parent"
ohos:margin="8vp"
ohos:text="Powered by Yandex.Translate"
ohos:text_size="18fp"
ohos:visibility="hide"
/>
<DirectionalLayout
ohos:id="$+id:avatarLayout"
ohos:height="0vp"
ohos:width="match_parent"
ohos:margin="8vp"
ohos:orientation="horizontal"
ohos:weight="3"
>
<Image
ohos:id="$+id:bigImage"
ohos:height="match_parent"
ohos:width="0vp"
ohos:image_src="$media:bot"
ohos:margin="8vp"
ohos:weight="1"
/>
<DependentLayout
ohos:id="$+id:videoLayout"
ohos:height="match_parent"
ohos:width="0vp"
ohos:orientation="horizontal"
ohos:visibility="hide"
ohos:weight="1"
>
</DependentLayout>
<ListContainer
ohos:id="$+id:chatList"
ohos:height="match_parent"
ohos:width="0vp"
ohos:weight="1"
/>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:responseLayout"
ohos:height="0vp"
ohos:width="match_parent"
ohos:orientation="horizontal"
ohos:weight="1"
>
<DirectionalLayout
ohos:id="$+id:responseImageLayout"
ohos:height="match_content"
ohos:width="match_content"
ohos:orientation="vertical"
>
<Image
ohos:id="$+id:responseImageView"
ohos:height="40vp"
ohos:width="40vp"
ohos:image_src="$media:bot"
ohos:scale_mode="zoom_center"
/>
<Text
ohos:id="$+id:statusText"
ohos:height="match_content"
ohos:width="40vp"
ohos:margin="2vp"
ohos:text=""
ohos:text_size="16fp"
/>
</DirectionalLayout>
<ScrollView
ohos:id="$+id:responseScroll"
ohos:height="match_parent"
ohos:width="match_parent"
>
<ohos.agp.components.webengine.WebView
ohos:id="$+id:responseText"
ohos:height="match_content"
ohos:width="match_content"
/>
</ScrollView>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:chatCLayout"
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="horizontal"
ohos:padding="10vp"
>
<TextField
ohos:id="$+id:editText"
ohos:height="match_parent"
ohos:width="0vp"
ohos:hint="you say"
ohos:text_size="16fp"
ohos:weight="1"
>
</TextField>
<Image
ohos:id="$+id:speakButton"
ohos:height="30vp"
ohos:width="30vp"
ohos:background_element="#FFFFFF"
ohos:image_src="$media:mic"
ohos:scale_mode="zoom_center"
/>
</DirectionalLayout>
<DirectionalLayout
ohos:height="1vp"
ohos:width="match_parent"
ohos:background_element="#d5d5d5"
ohos:bottom_margin="5vp"
ohos:orientation="horizontal"
/>
<DirectionalLayout
ohos:id="$+id:menuMLayout"
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="horizontal"
>
<Button
ohos:id="$+id:menuButton"
ohos:height="30vp"
ohos:width="30vp"
ohos:background_element="$media:menu"
ohos:margin="5vp"
ohos:padding="5vp"
/>
<Button
ohos:id="$+id:soundButton"
ohos:height="30vp"
ohos:width="30vp"
ohos:background_element="$media:sound"
ohos:margin="5vp"
/>
</DirectionalLayout>
</DirectionalLayout>
其中 对话列表展示为ListContainer:
<ListContainer
ohos:id="$+id:chatList"
ohos:height="match_parent"
ohos:width="0vp"
ohos:weight="1"
/>
2、在ChatAbility中实现语音对话
public void submitChat() {
ChatConfig config = new ChatConfig();
//config.instance = this.instance.id;
config.conversation = MainAbility.conversation;
config.speak = !MainAbility.deviceVoice;
config.avatar = this.avatarId;
if (MainAbility.translate && MainAbility.voice != null) {
config.language = MainAbility.voice.language;
}
if (MainAbility.disableVideo) {
config.avatarFormat = "image";
} else {
config.avatarFormat = MainAbility.webm ? "webm" : "mp4";
}
config.avatarHD = MainAbility.hd;
config.message = editText.getText().toString().trim();
if (config.message.equals("")) {
return;
}
this.messages.add(config);
handler.sendEvent(LIST_CONTAINER);
HttpChatAction action = new HttpChatAction(ChatAbility.this, config);
action.execute();
editText.setText("");
resetToolbar();
webView.load(null, "thinking...", "text/html", "utf-8", null);
//Check the volume
AudioManager audioManager = new AudioManager(ability);
int volume = 0;
try {
volume = audioManager.getVolume(AudioManager.AudioVolumeType.STREAM_MUSIC);
} catch (AudioRemoteException e) {
e.printStackTrace();
}
if (volume <= 1 && volumeChecked) {
ToastUtils.show(this, "Please check 'Media' volume");
volumeChecked = false;
}
//stop letting the mic on.
stopListening();
//its Important for "sleep" "scream" ...etc commands.
//this will turn off the mic
MainAbility.listenInBackground = false;
}
在sdk6,DevEco Studio2.2 Beta1下项目可直接运行,如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件,并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下
CodeCheck代码测试无异常
CloudTest代码测试无异常
病毒安全检测通过
当前版本demo功能与原组件基本无差异
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型