代码拉取完成,页面将自动刷新
本项目是基于开源项目speech进行ohos化的移植和开发的,可以通过项目标签以及github地址( https://github.com/gotev/android-speech )追踪到原项目版本
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
}
repositories {
maven {
url 'http://106.15.92.248:8081/repository/Releases/'
}
}
dependencies {
implementation 'net.gotev.ohos:speech:1.0.0'
}
使用该library之前,需要先在Ability中进行初始化
public class YourAbility extends Ability {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_activity_main);
Speech.init(this, getBundleName(), mTttsInitListener);
}
@Override
protected void onStop() {
super.onStop();
// prevent memory leaks when Ability is stopped
Speech.getInstance().shutdown();
}
}
在Ability内部添加如下代码:
try {
// 下面操作需要先申请权限 ohos.permission.MICROPHONE
Speech.getInstance().startListening(new SpeechDelegate() {
@Override
public void onStartOfSpeech() {
Log.i("speech", "speech recognition is now active");
}
@Override
public void onSpeechRmsChanged(float value) {
Log.d("speech", "rms is now: " + value);
}
@Override
public void onSpeechPartialResults(List<String> results) {
StringBuilder str = new StringBuilder();
for (String res : results) {
str.append(res).append(" ");
}
Log.i("speech", "partial result: " + str.toString().trim());
}
@Override
public void onSpeechResult(String result) {
Log.i("speech", "result: " + result);
}
});
} catch (SpeechRecognitionNotAvailable exc) {
Log.e("speech", "Speech recognition is not available on this device!");
}
布局中添加如下代码:
<DirectionalLayout
ohos:orientation="vertical"
ohos:width="match_content"
ohos:height="match_content"
ohos:id="$+id:linearLayout">
<net.gotev.speech.ui.SpeechProgressView
ohos:id="$+id:progress"
ohos:width="120vp"
ohos:height="150vp"/>
</DirectionalLayout>
需要注意的是 SpeechProgressView
需要添加到DirectionalLayout中以保证功能正常。
另外可以根据bar height settings(见下面说明)来调整宽高。
接下来可以传递 SpeechProgressView
参数来启动语音识别:
Speech.getInstance().startListening(speechProgressView, speechDelegate);
可以根据需要设置5中bar colors,代码如下:
int[] colors = {
getColor(ResourceTable.Color_black),
getColor(ResourceTable.Color_darker_gray),
getColor(ResourceTable.Color_black),
getColor(ResourceTable.Color_holo_orange_dark),
getColor(ResourceTable.Color_holo_red_dark)
};
speechProgressView.setColors(colors);
int[] heights = {60, 76, 58, 80, 55};
speechProgressView.setBarMaxHeightsInDp(heights);
在Ability中添加如下代码:
Speech.getInstance().say("say something");
也可以提供一个callback来接收状态:
Speech.getInstance().say("say something", new TextToSpeechCallback() {
@Override
public void onStart() {
Log.i("speech", "speech started");
}
@Override
public void onCompleted() {
Log.i("speech", "speech completed");
}
@Override
public void onError() {
Log.i("speech", "speech error");
}
});
在代码任何地方,都可以使用speech实例来配置各种参数:
Speech.getInstance()
完整的使用说明请参考JavaDocs.
默认情况下,该library的日志是关闭的,在代码任意地方都可以调用下面代码来打开调试日志:
Logger.setLogLevel(LogLevel.DEBUG);
日志级别从DEBUG到OFF都可以调整.
该库默认使用ohos.hiviewdfx.HiLog
来打印日志, 因此可以通过shell命令log
来获取打印输出。
如果要使用其他方式来记录日志,可以定制自己的日志代理实现,具体如下:
Logger.setLoggerDelegate(new Logger.LoggerDelegate() {
@Override
public void error(String tag, String message) {
//your own implementation here
}
@Override
public void error(String tag, String message, Throwable exception) {
//your own implementation here
}
@Override
public void debug(String tag, String message) {
//your own implementation here
}
@Override
public void info(String tag, String message) {
//your own implementation here
}
});
Copyright (C) 2019 Aleksandar Gotev
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。