2 Star 0 Fork 0

hihopeorg / ohos-speech

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 6.78 KB
一键复制 编辑 原始数据 按行查看 历史
shaojunye 提交于 2021-10-15 14:48 . update README

ohos-speech

本项目是基于开源项目speech进行ohos化的移植和开发的,可以通过项目标签以及github地址( https://github.com/gotev/android-speech )追踪到原项目版本

项目介绍

  • 项目名称:ohos-speech
  • 所属系列:ohos的第三方组件适配移植
  • 功能:ohos-speech是一个将语音识别和文本语音转换变得简单的库。
  • 项目移植状态:完成
  • 调用差异:无
  • 项目作者和维护人:hihope
  • 联系方式:hihope@hoperun.com
  • 原项目Doc地址:https://github.com/gotev/android-speech
  • 原项目基线版本:v1.6.2, sha1:292b1713155a7d551ddb5846bac0316461a0369d
  • 编程语言:Java
  • 外部库依赖:无

效果展示

p1

安装教程

方法一:
  1. 下载har包speech.har。
  2. 启动 DevEco Studio,将下载的har/jar包,导入工程目录“entry->libs”下。
  3. 在moudle级别下的build.gradle文件中添加依赖,在dependences标签中增加对libs目录下har/jar包的引用。
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
}
  1. 在导入的har包上点击右键,选择“Add as Library”对包进行引用,选择需要引用的模块,并点击“OK”即引用成功。
方法二:
  1. 在工程的build.gradle的allprojects中,添加HAR所在的Maven仓地址:
 repositories {
     maven {
         url 'http://106.15.92.248:8081/repository/Releases/' 
     }
 }
  1. 在应用模块的build.gradle的dependencies闭包中,添加如下代码:
 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);
设置自定义 bar colors

可以根据需要设置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);
自定义 bar height最大值
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.

Logging

默认情况下,该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
    }
});

版本迭代

  • v1.0.0
    • 语音转文字
    • 文字转语音

版权和许可信息

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.
1
https://gitee.com/hihopeorg/ohos-speech.git
git@gitee.com:hihopeorg/ohos-speech.git
hihopeorg
ohos-speech
ohos-speech
master

搜索帮助