gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:12:43 25 4
gpt4 key购买 nike

我正在研究 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com