语音识别和文本转语音

2024-06-04

我想开发一个实现语音识别的应用程序,然后使用文本到语音引擎实现文本到语音。我发布了下面的代码。我使用两个按钮和一个列表视图。一个按钮用于语音识别,另一个按钮用于文本转语音,列表视图用于两者(首先在列表视图中发布语音识别的结果,然后应用程序将从列表视图中读回单词)。当我触摸语音识别按钮时,单词会发布在我的列表视图中,但问题是,当我按下文本转语音按钮时,应用程序不会从列表视图和我的 logcat 中读回单词我按下此按钮我没有收到任何有关此的信息。 这是我的程序:

package rtv.rtv.rtv;

import android.app.Activity;
import android.os.Bundle;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class VoiceRecTextSpeech extends Activity implements OnClickListener,OnInitListener {
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    private ListView mList;

    private Button speakBtn = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
    private static final String TAG = "TTS Demo";
    private TextToSpeech mTts;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Button and ListView for Voice Recognition
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);

        //Button for Text to Speech
        speakBtn = (Button)findViewById(R.id.speak);

        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }

        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }


        //Handle the click on the start recognition button and on text to speech button

        public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btn_speak:
            startVoiceRecognitionActivity();
            break;
        case R.id.speak:
            mTts.speak(mList.toString(), TextToSpeech.QUEUE_ADD, null);
            break;

        }
        }

        //Fire an intent to start the speech recognition activity

        private void startVoiceRecognitionActivity() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
        }

        // Handle the results 

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
                // Fill the list view with the strings the recognizer thought it could have heard
                ArrayList<String> matches = data.getStringArrayListExtra(
                        RecognizerIntent.EXTRA_RESULTS);
                mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                        matches));
            }

            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == REQ_TTS_STATUS_CHECK) {
                switch (resultCode) {
                case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                    // TTS is up and running
                    mTts = new TextToSpeech(this, this);
                    Log.v(TAG, "Pico is installed okay");
                    break;
                case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
                case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
                case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                    // missing data, install it
                    Log.v(TAG, "Need language stuff: " + resultCode);
                    Intent installIntent = new Intent();
                    installIntent.setAction(
                            TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                    break;
                case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
                default:
                    Log.e(TAG, "Got a failure. TTS apparently not available");
                }
            }
            else {
                // Got something else
            }

        }

         @Override
            public void onInit(int status) {
                // Now that the TTS engine is ready, we enable the button
                if( status == TextToSpeech.SUCCESS) {
                    speakBtn.setEnabled(true);
                }
            }
            @Override
            public void onPause()
            {
                super.onPause();
                // if we're losing focus, stop talking
                if( mTts != null)
                    mTts.stop();
            }
            @Override
            public void onDestroy()
            {
                super.onDestroy();
                mTts.shutdown();
            }
}

这是 main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button android:id="@+id/speak"
        android:layout_height="wrap_content"
        android:enabled="false" android:text="Text To Speech" android:layout_width="match_parent"/>

    <Button android:id="@+id/btn_speak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="Speak for Voice Recognition"/>

    <ListView android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    </LinearLayout>

Thanks !


您的 TTS 按钮没有注册 onClickListener()。因此,您的启动 TTS 的代码永远不会被调用。您确定要将 ListView 转换为字符串并将其传递给 TTS 引擎吗?更有可能的是,您希望将 ListView 适配器中的数据转换为字符串。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

语音识别和文本转语音 的相关文章

随机推荐

  • 想要从模板创建 StackDriver 仪表板

    是否可以通过声明性模板以编程方式创建 StackDriver 仪表板 类似于 Google Cloud Deployment 允许您创建和管理云资源的方式 我们为每个环境 master staging prod 使用不同的 GCP 项目 并
  • 带有导出字段的私有类型

    在 Go 教程的第二天有这样的练习 为什么拥有带有导出字段的私有类型会很有用 例如 package geometry type point struct X Y int name string 请注意point是小写的 因此不会导出 而字段
  • XML 模式不区分大小写的简单类型字符串枚举

    我的 XML 架构 xsd 文件中需要不区分大小写的字符串枚举类型 我可以通过执行以下操作来不区分大小写
  • Log4Perl 将多个程序的日志记录捆绑到一个日志中

    CPAN 上是否有任何 Logger 它允许我将多个程序的日志捆绑到一个文件中 并在两个程序同时运行并并行调用 log4Perl 时同步并行日志记录 背景是我使用一个自定义附加程序来写入电子邮件 我想将所有电子邮件捆绑在一个文件中作为备份
  • 如何在 django 表单中设置自定义 HTML 属性?

    我有一个 Django 表单 它是页面的一部分 假设我有一个字段 search input forms CharField u Search word required False 我只能通过模板访问它 form search input
  • django/whitenoise 存储后端导致错误

    我在调试关闭时在 heroku 上运行 django 应用程序时遇到了 500 错误 使用 rollbar 了解发生错误的原因后 它报告了以下内容 ValueError The file media img 1 jpg could not
  • 如何禁用 NSDictionary 自动按键排序

    当我添加值时NSMutableDictionary它会自动设置密钥 我如何禁用它并按照第一组第一和第二组第二排列 NSMutableDictionary filteredDictionary NSMutableDictionary dict
  • 使用 Canon EDSDK 2.5.2 VB.NET 实时查看

    我正在尝试做两件事中的一件 第一件事 使用 VB NET 和 Canon EDSDK 2 5 2 打开实时视图 并在 Windows 窗体应用程序中渲染实时输出 目前我正在尝试将其放入图片框 不过 我肯定愿意接受建议 第二个选项是至少打开实
  • 在 Nano 中更新 CouchDB 文档 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我需要获取一个文档 更改 插入 删除
  • 实体框架 - sql server 表中未设置默认值

    SQL Server 2005 数据库表有一列 createdon 其默认值设置为 getdate 我正在尝试使用实体框架添加记录 createdon 列未更新 我是否错过了实体框架中的任何属性 请提出建议 这是实体框架存在的少数问题之一
  • 从 java 类生成 xsd 的实用程序

    我想为以下类生成 xsd public class Node private String value private List
  • 如何在自定义保存操作 WFFM 中获取 Sitecore.Current.Site 对象?

    我在用着面向营销人员的 Sitecore 网络表单 在里面save action我得到的表格Sitecore Context Site对象 但该对象没有返回正确的上下文 该值为 modules shell 有谁知道我如何才能获得正确的上下文
  • Plotly - 不同颜色的表面

    我正在尝试在 Plotly for Python 中绘制多个曲面 每个曲面具有不同的颜色 具体来说 表面显示了在相空间中不同点采取行动的预测奖励函数 由于我在每个点都有多个可能的操作 因此每个点都是不同的表面 我想对每个表面进行独特的着色
  • svg路径指针事件-点击检测

    我正在编写一些 HTML 以便可以使用 HTML SVG 和 PATH 标签绘制贝塞尔曲线 我的曲线效果非常好 现在我想添加一项功能 如果用户将鼠标悬停在曲线上 我会更改颜色 但实际情况是 SVG 创建了一个包含路径的大框 并捕获所有点击
  • java.time.LocalDate 到 java.util.Date

    转换的最佳方式是什么java time LocalDate to java util Date Date from dateToReturn atStartOfDay ZoneId systemDefault toInstant 我一直在尝
  • 在 Grand Central Dispatch 中使用dispatch_sync

    任何人都可以用非常清晰的用例解释其目的是什么dispatch sync in GCD是为了 我不明白在哪里以及为什么我必须使用它 Thanks 当您想要执行一个块并等待结果时可以使用它 其中一个示例是使用调度队列而不是锁进行同步的模式 例如
  • 如何在 CSS 中使用 3 位颜色代码而不是 6 位颜色代码?

    我最近检查了我的 CSS 文件 并将所有六位十六进制代码转换为简单的三位数代码 例如 我的 FDFEFF被缩短为 FFF 它呈现的颜色与以前几乎完全相同 在我看来 中间部分相当无用 删除它们在我的 CSS 文件中节省了整整 300 个字节
  • 你好世界,裸机 Beagleboard

    我正在尝试在我的 Beagleboard xm rev 上运行 hello world 类型的程序 C 通过调用 Cputs功能来自装配 到目前为止 我一直使用这个作为参考 http wiki osdev org ARM Beagleboa
  • 自定义 Sql Server 对象资源管理器右键单击菜单项

    如何在 Sql Server 2012 的对象资源管理器中添加或自定义右键菜单项 例如 我想将新项目添加到表右键菜单中以生成自定义表创建器脚本 您可以编写一个 SSMS 加载项 See http sqlblogcasts com blogs
  • 语音识别和文本转语音

    我想开发一个实现语音识别的应用程序 然后使用文本到语音引擎实现文本到语音 我发布了下面的代码 我使用两个按钮和一个列表视图 一个按钮用于语音识别 另一个按钮用于文本转语音 列表视图用于两者 首先在列表视图中发布语音识别的结果 然后应用程序将