语音识别和文本到语音转换 [英] Voice Recognition and Text to Speech

查看:170
本文介绍了语音识别和文本到语音转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发它实现了语音识别应用程序,之后使用文本到语音Engine.I贴code波纹管实现文本到语音。我使用两个按钮,用于语音识别的列表view.One按钮,另一个被用于文本到语音,和列表视图用于两个(第一列表视图中被张贴语音识别的结果,然后该应用程序将回读从列表视图的话)。当我触摸了语音识别的字贴在我的列表视图按钮,但问题是,当我preSS的按钮,文本到语音的应用程序不会从列表视图和回读的话我logcat的时候我preSS这个按钮,我没有收到有关这方面的消息。 这是我的计划:

I want to develop an application which implements voice recognition,and after that implements text to speech using text to speech Engine.I posted the code bellow. I use two buttons and a list view.One button is used for voice recognition, another one is used for text to speech, and the list view is used for both (first in the list view is posted the result of voice recognition, and then the application will read back the words from the list view). When I touch the button for the voice recognition the words are posted in my list view, but the problem is that when I press the button for text to speech the application doesn't read back the words from the list view and in my logcat when I press this button I don't receive any information about this. Here is my program:

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中:

Here is the 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>

谢谢!

推荐答案

即使状态== TextToSpeech.SUCCESS的推移,它可能是不支持的语言数据。

Even if status == TextToSpeech.SUCCESS passes, it could be that the language data is not supported.

有时后的OnInit(),您需要执行一些code,它是这样的:

Sometime after onInit() you need to execute some code that looks like this:

public void onInit(Context context, TextToSpeech tts, Locale forLocale)
{
    Locale defaultOrPassedIn = forLocale;
    if (forLocale == null)
    {
        defaultOrPassedIn = Locale.getDefault();
    }
    // check if language is available
    switch (tts.isLanguageAvailable(defaultOrPassedIn))
    {
        case TextToSpeech.LANG_AVAILABLE:
        case TextToSpeech.LANG_COUNTRY_AVAILABLE:
        case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
            tts.setLanguage(defaultOrPassedIn);
            break;
        case TextToSpeech.LANG_MISSING_DATA:
            Log.d(TAG, "MISSING_DATA");
            // check if waiting....
            Intent installIntent = new Intent();
            installIntent
                    .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            context.startActivity(installIntent);
            break;
        case TextToSpeech.LANG_NOT_SUPPORTED:
            Log.d(TAG, "NOT SUPPORTED");
            // report failure to the user
            break;
    }
}

这篇关于语音识别和文本到语音转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆