android - 如何在android中一一使用具有多个值的文本语音

标签 android text-to-speech

我正在研究 TTS(文本到语音),我已经从互联网上下载了一个演示。我已经成功运行了它。我现在想修改它,我在屏幕上有多个值,并且希望每个词都单独说出来。

这是我的代码:

package com.androidhive.texttospeech;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
    /**
     * Called when the activity is first created.
     */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    String one, two, three, four;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        one = "Hello";
        two = "how";
        three = "are";
        four = "you";

        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);

        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            // tts.setPitch(5); // set pitch level

            // tts.setSpeechRate(2); // set speech speed rate

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed");
        }

    }

    private void speakOut() {

        private void speakOut () {

            String que = answer_question.getText().toString();
            String op1 = opt_1.getText().toString();
            String op2 = opt_2.getText().toString();
            String op3 = opt_3.getText().toString();
            String op4 = opt_4.getText().toString();
            tts.speak(que, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op1, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op2, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op3, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op4, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

最佳答案

试试这个。

        private void speakOut () {

            String que = answer_question.getText().toString();
            String op1 = opt_1.getText().toString();
            String op2 = opt_2.getText().toString();
            String op3 = opt_3.getText().toString();
            String op4 = opt_4.getText().toString();
            tts.speak(que, TextToSpeech.QUEUE_ADD, null);
            tts.speak(op1, TextToSpeech.QUEUE_ADD, null);
            tts.speak(op2, TextToSpeech.QUEUE_ADD, null);
            tts.speak(op3, TextToSpeech.QUEUE_ADD, null);
            tts.speak(op4, TextToSpeech.QUEUE_ADD, null);
        }

它会按顺序说出每个单词。

或者,如果您想在说出前一个词后说出下一个词,请尝试像这样使用 UtteranceProgressListener

package com.androidhive.texttospeech;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
/**
 * Called when the activity is first created.
 */

private HashMap<String, String> utterParam;
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

String one, two, three, four;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    one = "Hello";
    two = "how";
    three = "are";
    four = "you";

    tts = new TextToSpeech(this, this);

    btnSpeak = (Button) findViewById(R.id.btnSpeak);

    txtText = (EditText) findViewById(R.id.txtText);

    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();
        }

    });
}

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub
    utterParam = new HashMap<String, String>();
    utterParam.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utterID");

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            speakOut();
        }

    } else {
        Log.e("TTS", "Initilization Failed");
    }

}

private void speakOut() {

    private void speakOut () {

        String que = answer_question.getText().toString();
        //String op1 = opt_1.getText().toString();
        //String op2 = opt_2.getText().toString();
        //String op3 = opt_3.getText().toString();
        //String op4 = opt_4.getText().toString();
        tts.speak(que, TextToSpeech.QUEUE_FLUSH, utterParam);
        //tts.speak(op1, TextToSpeech.QUEUE_FLUSH, null);
        //tts.speak(op2, TextToSpeech.QUEUE_FLUSH, null);
        //tts.speak(op3, TextToSpeech.QUEUE_FLUSH, null);
        //tts.speak(op4, TextToSpeech.QUEUE_FLUSH, null);
    }
}

private UtterProgressListener utterListener = new UtterProgressListener() {
    @Override
    public void onDone(String utteranceId) {
        // TODO Auto-generated method stub
        if (utteranceId.equals("utterID")) {
            // This code will be activate when TextToSpeech is stopped or done.(under API 22)
            // This code will be activate when TextToSpeech is done.(over API 23)
        }
    }

    @Override
    public void onError(String utteranceId) {
        // TODO Auto-generated method stub
        if (utteranceId.equals("utterID")) {

        }
    }

    @Override
    public void onStart(String utteranceId) {
        // TODO Auto-generated method stub
        if (utteranceId.equals("utterID")) {

        }
    }

    @TargetApi(23)
    @Override
    public void onStop(String utteranceId, boolean interrupted) {
        if (utteranceId.equals("utterID")) {
              // This code will be activate when TextToSpeech is stopped.(overr API 23)
        }
    }
};

关于android - 如何在android中一一使用具有多个值的文本语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525581/

相关文章:

android - 使用警报对话框生成器的 fragment 上的日期选择器对话框

C#语音合成

android - 从 React Native 中的变量播放音频

c# - Ivona 请求签名问题 - 签名不匹配(AWS 签名版本 4)

text-to-speech - pyttsx : No module named 'engine'

java - Firebase 数据库和 Firebase Firestore 数据操作和检索数据的问题

Android(java) 相当于 NSString stringByAddingPercentEscapesUsingEncoding :?

android - 即使添加到 gradle 后仍 Unresolved reference

android - 处理高分辨率图像

javascript - 如何在没有重定向的情况下执行 href