Android TTS ONUTERNANCE已完成回调是';我没接到电话
我试图让Android TTS API读取我的“话语”,然后调用onutternanceCompleted()监听器,但没有成功。我已经注册了我的TTS对象,它返回SUCCESS,所以我一辈子都不明白为什么我的回调没有被调用 我试过寻求帮助,但似乎其他人也有困难。我错过了一些简单的东西吗 谢谢你能提供的任何帮助Android TTS ONUTERNANCE已完成回调是';我没接到电话,android,text-to-speech,Android,Text To Speech,我试图让Android TTS API读取我的“话语”,然后调用onutternanceCompleted()监听器,但没有成功。我已经注册了我的TTS对象,它返回SUCCESS,所以我一辈子都不明白为什么我的回调没有被调用 我试过寻求帮助,但似乎其他人也有困难。我错过了一些简单的东西吗 谢谢你能提供的任何帮助 package com.test.mytts; import java.util.HashMap; import android.app.Activity; import andro
package com.test.mytts;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{
TextView tv;
private TextToSpeech _tts;
@Override
public void onCreate(Bundle savedInstanceState)
{
tv = new TextView(this);
tv.setText("MyTTS: ");
super.onCreate(savedInstanceState);
setContentView(tv);
_tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int status)
{
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");
if (status == TextToSpeech.SUCCESS)
{
Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();
int result = _tts.setOnUtteranceCompletedListener(this);
tv.append(String.valueOf(result));
_tts.setSpeechRate((float) .5);
_tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
}
else
Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();
}
@Override
public void onUtteranceCompleted(String utteranceId)
{
Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy()
{
super.onDestroy();
_tts.shutdown();
}
}
package com.test.mytts;
导入java.util.HashMap;
导入android.app.Activity;
导入android.media.AudioManager;
导入android.os.Bundle;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.speech.tts.TextToSpeech.onutternancecompletedlistener;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MyTTS扩展活动实现OnInitListener、OnPatternanceCompletedListener
{
文本视图电视;
私人短信通话系统;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
tv=新文本视图(此);
tv.setText(“MyTTS:”);
super.onCreate(savedInstanceState);
设置内容视图(电视);
_tts=新的TextToSpeech(这个,这个);
}
@凌驾
公共无效onInit(int状态)
{
HashMap myHashAlarm=新建HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashAlarm.put(TextToSpeech.Engine.KEY\u PARAM\u outrance\u ID,“test”);
if(status==TextToSpeech.SUCCESS)
{
Toast.makeText(这是“试图说话…”),Toast.LENGTH_SHORT.show();
int result=\u tts.setonutternancecompletedlistener(这个);
append(String.valueOf(result));
_tts.setSpeechRate((浮动).5);
_tts.speak(“测试一、二、三”,TextToSpeech.QUEUE\u ADD,myHashAlarm);
}
其他的
Toast.makeText(这是“未能初始化TTS.”,Toast.LENGTH_SHORT.show();
}
@凌驾
公共void onutternancecompleted(字符串utternanceid)
{
Toast.makeText(这个“onutternancecompleted”,Toast.LENGTH_SHORT.show();
}
@凌驾
公共空间
{
super.ondestory();
_tts.shutdown();
}
}
我认为,除非您指定一个带有id的语句,例如:
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceid);
您的话语完成方法将不会被调用
在本例中,map是您说话时传递给引擎的Hashmap。在tts对象的onInit函数内调用setOnPatternanceCompletedListener 如果要在调用onutternancecompleted函数时对UI进行任何更改,请在runOnUIThread方法中添加代码 请记住在调用speak()函数时添加Hashmap param值 例如:
TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
@Override
public void onInit(int status) {
mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//UI changes
}
});
}
});
}
});
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
TextToSpeech tts=new TextToSpeech(上下文,new OnInitListener()){
@凌驾
公共无效onInit(int状态){
mTts.setOnutternanceCompletedListener(新的OnutternanceCompletedListener(){
@凌驾
公共void onutternancecompleted(字符串utternanceid){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//用户界面更改
}
});
}
});
}
});
HashMap params=新的HashMap();
参数put(TextToSpeech.Engine.KEY_PARAM_outrance_ID,“stringId”);
tts.speak(“Text to speak”,TextToSpeech.QUEUE_FLUSH,params);
这将在API级别>=15时适用
import java.util.HashMap;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnInitListener{
private static final int CHECK_TTS_DATA = 0X123;
protected static final String TAG = MainActivity.class.getSimpleName();
private TextToSpeech textToSpeech;
private Button buttonSayIt;
private EditText editTextTts;
String tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSayIt=(Button) findViewById(R.id.buttonSayIt);
editTextTts=(EditText) findViewById(R.id.editTextTts);
buttonSayIt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tts=editTextTts.getText().toString();
Log.d(TAG, tts);
speach(tts,"you_utterance_id");
}
});
//check for TTs data
Intent checkTtsDataIntent=new Intent();
checkTtsDataIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTtsDataIntent, CHECK_TTS_DATA);
}
protected void speach(String tts,String utteranceId) {
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,utteranceId);
textToSpeech.speak(tts,TextToSpeech.QUEUE_FLUSH,params);
}
@Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
if(textToSpeech.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE){
textToSpeech.setLanguage(Locale.US);
}
}else if(status==TextToSpeech.ERROR){
Toast.makeText(this, "Sorry Text To Speach faild", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==CHECK_TTS_DATA){
if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
textToSpeech=new TextToSpeech(this, this);
textToSpeech.setOnUtteranceProgressListener(utteranceProgressListener);
}else{
Intent installTtsIntent=new Intent();
installTtsIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTtsIntent);
}
}
}
UtteranceProgressListener utteranceProgressListener=new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d(TAG, "onStart ( utteranceId :"+utteranceId+" ) ");
}
@Override
public void onError(String utteranceId) {
Log.d(TAG, "onError ( utteranceId :"+utteranceId+" ) ");
}
@Override
public void onDone(String utteranceId) {
Log.d(TAG, "onDone ( utteranceId :"+utteranceId+" ) ");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import java.util.HashMap;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.speech.tts.utranceProgressListener;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展活动实现OnInitListener{
私有静态最终整数检查\u TTS\u数据=0X123;
受保护的静态最终字符串标记=MainActivity.class.getSimpleName();
私有texttospeechtexttospeech;
私人按钮;
私人编辑文本编辑文本;
字符串tts;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSayIt=(Button)findViewById(R.id.buttonSayIt);
editTextTts=(EditText)findViewById(R.id.editTextTts);
buttonSayIt.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
tts=editTextTts.getText().toString();
Log.d(标签,tts);
演讲(tts,“你的话语”);
}
});
//检查TTs数据
意向检查TTSDataint=新意向();
检查TTSDattainment.setAction(text到speech.Engine.ACTION\u检查TTS\u数据);
startActivityForResult(检查污染、检查数据);
}
受保护的void speach(字符串tts、字符串outtanceid){
HashMap params=新的HashMap();
参数put(TextToSpeech.Engine.KEY_PARAM_outrance_ID,outranceid);
textToSpeech.speak(tts,textToSpeech.QUEUE_FLUSH,params);
}
@凌驾
公共无效onInit(int状态){
if(status==TextToSpeech.SUCCESS){
if(textToSpeech.islanguage可用(Locale.US)=textToSpeech.LANG\u可用){
textToSpeech.setLanguage(Locale.US);
}
}else if(状态==TextToSpeech.ERROR){
Toast.makeText(这是“抱歉的讲话失败的文本”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==检查TTS数据){
if(resultCode==TextToS
textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int result=textToSpeech.setLanguage(Locale.ENGLISH);
if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.i("TextToSpeech","Language Not Supported");
}
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.i("TextToSpeech","On Start");
}
@Override
public void onDone(String utteranceId) {
Log.i("TextToSpeech","On Done");
}
@Override
public void onError(String utteranceId) {
Log.i("TextToSpeech","On Error");
}
});
}else {
Log.i("TextToSpeech","Initialization Failed");
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}