- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简介
我正在开发一个需要使用 booth SpeechRecognizer 和 TTS 的应用程序。但是我在尝试这个时遇到了一些问题。最主要的是,如果我初始化 TTS,SpeechRecgonizer 似乎不起作用,如果我禁用 TTS,那么 SpeechRecognizer 工作正常。接下来是带有相关代码的代码 fragment :
代码
public class GameActivity extends Activity implements OnInitListener {
private static TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
/*SPEECH RECOGNIZER INSTANT*/
Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
/*START LISTENING*/
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
/*CHECK TTS AVAILABLE*/
Intent checkIntent = new Intent();
checkIntent.setAction( TextToSpeech.Engine.ACTION_CHECK_TTS_DATA );
startActivityForResult(checkIntent, CHECK_TTS_DATA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CHECK_TTS_DATA: {
/*IF OK, INITIALIZES TTS*/
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
Log.d("TTS", "TTS available");
tts = new TextToSpeech(this, this);
}
else {
Log.d("TTS", "TTS NOT available");
Intent promptInstall = new Intent();
promptInstall.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(promptInstall);
}
break;
}
}
}
/**OnInitListener implementation*/
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
/*set Language*/
tts.setLanguage(Locale.getDefault());
/*STARTS MAIN METHOD*/
Log.d("SPEECH", "Initialized, starting main method");
SpeechWhenMotion();
} else {
Log.e("TTS", "Initilization Failed");
}
}
/**INNER LISTENER CLASS*/
private class SpeechListener implements RecognitionListener {
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("SR_LISTENER", "onBufferReceived");
}
@Override
public void onError(int error) {
Log.d("SR_LISTENER", "onError: " + error);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("SR_LISTENER", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("SR_LISTENER", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("SR_LISTENER", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("SR_LISTENER", "onResult");
matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("SR_LISTENER", "onRmsChanged");
}
@Override
public void onBeginningOfSpeech() {
Log.d("SR_LISTENER", "onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech() {
Log.d("SR_LISTENER", "onEndOfSpeech");
}
}
所以基本上我面对的是介绍中解释的内容。如果我不执行 tts = new TextToSpeech(this, this);
,那么我会在 LogCat 中看到引用语音监听器的日志,但是如果我初始化 TTS,我就看不到这个日志。
另外,我想当我能够让它们工作时,我应该停止听 TTS 讲话。
更新——完整的简单项目代码
主要 Activity :
public class MainActivity extends Activity implements OnInitListener {
/*Voice and speech variables*/
private static TextToSpeech tts;
public static final int CHECK_TTS_DATA = 1;
public static boolean fase0 = true;
public static boolean fase1 = false;
/*Service*/
private int mBindFlag;
public static Messenger mActivityMessenger;
static int result;
private final boolean mFinished = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivityMessenger = new Messenger(new IncomingHandler());
Intent serviceIntent = new Intent(this, SpeechRecognitionService.class);
serviceIntent.putExtra("Messenger", mActivityMessenger);
startService(serviceIntent);
mBindFlag = Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH ? 0 : Context.BIND_ABOVE_CLIENT;
Intent checkIntent = new Intent();
checkIntent.setAction( TextToSpeech.Engine.ACTION_CHECK_TTS_DATA );
startActivityForResult(checkIntent, CHECK_TTS_DATA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CHECK_TTS_DATA: {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
Log.d("TTS", "TTS available");
tts = new TextToSpeech(this, this);
}
else {
Log.d("TTS", "TTS NOT available");
Intent promptInstall = new Intent();
promptInstall.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(promptInstall);
}
break;
}
}
}
/******************
*
* MAIN METHOD
*
******************/
public void SpeechWhenMotion() {
while (!mFinished) {
if (fase0) {
tts.speak("Initializing", TextToSpeech.QUEUE_FLUSH, null);
fase0 = false;
}
else if (fase1) {
if (result == SpeechRecognitionService.CONTINUE) {
tts.speak("Correct", TextToSpeech.QUEUE_FLUSH, null);
mFinished = true;
}
else if (result == SpeechRecognitionService.NO_MATCH) {
tts.speak("No matching", TextToSpeech.QUEUE_FLUSH, null);
}
else {
Log.d("RESULT", String.valueOf(result));
}
}
}
}
/******************
*
* SERVICE LINK
*
******************/
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("SERVICE", "onServiceConnected"); //$NON-NLS-1$
SpeechRecognitionService.mServiceMessenger = new Messenger(service);
Message msg = new Message();
msg.what = SpeechRecognitionService.MSG_RECOGNIZER_START_LISTENING;
try {
SpeechRecognitionService.mServiceMessenger.send(msg);
}
catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("SERVICE", "onServiceDisconnected");
SpeechRecognitionService.mServiceMessenger = null;
}
};
private static class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SpeechRecognitionService.MSG_SPEECH_RESULT:
Log.d("MESSENGER", "Message received");
result = msg.arg1;
break;
default:
super.handleMessage(msg);
}
}
}
/******************
*
* IMPLEMENTED METHODS
*
******************/
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
/*set Language*/
tts.setLanguage(Locale.getDefault());
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
/*Start main method*/
Log.d("TTS", "Initialized, starting main method");
SpeechWhenMotion();
} else {
Log.e("TTS", "Initilization Failed");
}
}
/******************
*
* LIFE CYCLE
*
******************/
@Override
public void onDestroy() {
super.onDestroy();
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
@Override
protected void onStart() {
super.onStart();
/*Bind SpeechRecognition service*/
bindService(new Intent(this, SpeechRecognitionService.class), mServiceConnection, mBindFlag);
}
@Override
protected void onStop() {
super.onStop();
/*Unbind SpeechRecognition service*/
if (mActivityMessenger != null) {
unbindService(mServiceConnection);
mActivityMessenger = null;
}
}
}
服务:
public class SpeechRecognitionService extends Service {
protected static AudioManager mAudioManager;
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
public static Messenger mServiceMessenger;
protected boolean mIsListening;
protected volatile boolean mIsCountDownOn;
private static boolean mIsStreamSolo;
static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;
static final int MSG_SPEECH_RESULT = 3;
static ArrayList<String> matches;
static final int CONTINUE = 11;
static final int NO_MATCH = 12;
/******************
*
* ONCREATE
*
******************/
@Override
public void onCreate() {
super.onCreate();
mServiceMessenger = new Messenger(new IncomingHandler(this));
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("SERVICE", "onStartCommand");
MainActivity.mActivityMessenger = intent.getParcelableExtra("Messenger");
return super.onStartCommand(intent, flags, startId);
}
/******************
*
* METHODS
*
******************/
@Override
public IBinder onBind(Intent intent) {
Log.d("BIND", "onBind");
return mServiceMessenger.getBinder();
}
/**Count down timer for Jelly Bean work around*/
protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
mIsCountDownOn = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
try {
mServiceMessenger.send(message);
message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
mServiceMessenger.send(message);
}
catch (RemoteException e) {
}
}
};
public void sendMessageToUI(int result) {
Message msg = new Message();
msg = Message.obtain(null, MSG_SPEECH_RESULT);
msg.arg1 = result;
try {
MainActivity.mActivityMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
/******************
*
* INNER CLASSES/LISTENERS
*
******************/
protected static class IncomingHandler extends Handler {
private final WeakReference<SpeechRecognitionService> mtarget;
IncomingHandler(SpeechRecognitionService target) {
mtarget = new WeakReference<SpeechRecognitionService>(target);
}
@Override
public void handleMessage(Message msg) {
final SpeechRecognitionService target = mtarget.get();
switch (msg.what) {
case MSG_RECOGNIZER_START_LISTENING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
/*turn off beep sound*/
if (!mIsStreamSolo) {
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
mIsStreamSolo = true;
}
}
if (!target.mIsListening) {
target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
target.mIsListening = true;
Log.d("SERVICE", "message start listening"); //$NON-NLS-1$
}
break;
case MSG_RECOGNIZER_CANCEL:
if (mIsStreamSolo) {
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
mIsStreamSolo = false;
}
target.mSpeechRecognizer.cancel();
target.mIsListening = false;
Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
break;
}
}
}
protected class SpeechRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
/*speech input will be processed, so there is no need for count down anymore*/
if (mIsCountDownOn) {
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
Log.d("SR_LISTENER", "onBeginingOfSpeech"); //$NON-NLS-1$
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
Log.d("SR_LISTENER", "onEndOfSpeech"); //$NON-NLS-1$
}
@Override
public void onError(int error) {
if (mIsCountDownOn) {
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
mIsListening = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
try {
mServiceMessenger.send(message);
}
catch (RemoteException e) {
}
String issue = null;
if (error == 1) {
issue = "ERROR_NETWORK_TIMEOUT";
}
if (error == 2) {
issue = "ERROR_NETWORK";
}
if (error == 3) {
issue = "ERROR_AUDIO";
}
if (error == 4) {
issue = "ERROR_SERVER";
}
if (error == 5) {
issue = "ERROR_CLIENT";
}
if (error == 6) {
issue = "ERROR_SPEECH_TIMEOUT";
}
if (error == 7) {
issue = "ERROR_NO_MATCH";
}
if (error == 8) {
issue = "ERROR_RECOGNIZER_BUSY";
}
if (error == 9) {
issue = "ERROR_INSUFFICIENT_PERMISSIONS";
}
Log.d("SR_LISTENER", "error = " + issue); //$NON-NLS-1$
}
@Override
public void onEvent(int eventType, Bundle params) {
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onReadyForSpeech(Bundle params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mIsCountDownOn = true;
mNoSpeechCountDown.start();
}
Log.d("SR_LISTENER", "onReadyForSpeech"); //$NON-NLS-1$
}
@Override
public void onResults(Bundle results) {
Log.d("SR_LISTENER", "onResults"); //$NON-NLS-1$
matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches.contains("continue")) {
MainActivity.fase1 = true;
sendMessageToUI(CONTINUE);
}
else {
sendMessageToUI(NO_MATCH);
}
}
@Override
public void onRmsChanged(float rmsdB) {
}
}
/******************
*
* LIFE CYCLE
*
******************/
@Override
public void onDestroy() {
super.onDestroy();
if (mIsCountDownOn) {
mNoSpeechCountDown.cancel();
}
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
}
}
}
来自调试器的日志:
如上所示,服务监听器启动并显示 onReadyForSpeech,但在此旁边 TTS 已初始化,即使我说话,监听器也停止显示日志,然后应该显示 onBeginingOfSpeech
最佳答案
您应该实现 onUtteranceCompletedListener
并在 onUtteranceCompleted
中发送 MSG_RECOGNIZER_START_LISTENING。
关于android - 同时进行语音转文本和文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23650973/
我已经在标准 WPF 控件中实现了一个报告,并且还实现了一个 DocumentPaginator获取这些控件并将它们转换为用于打印的文档。 我还实现了一些使用文档分页器将页面呈现为图像并使用 PDFS
在 C# 中,我有以下代码: public static string GetHashCode(string p) { var a = new SHA256Managed();
您好,我正在尝试在编码后将我的 mysqli 数据库输出到一个 js 文件,我用 json_encode 对其进行编码没有任何问题,但是如何将其放入 js 文件中(每次更新时更新) mysqli数据已
我需要将 select 从 JS 传递到 HTML。 select 应该包含来自 PHP 的 option。 所以,首先我有一个 HTML div,我将在其中添加来自 JS 的内容。
我有一个相当大且复杂的 SVG 代码,它根据页面信息使用 JavaScript 和 jQuery 动态生成。 然后我有一个 AJAX 帖子保存。 我无法将其转换为正确发布图像数据? var canva
我想将我的本地日期 ([NSDate date]) 转换为 GMT 以创建一个 JSON 字符串 (/Date(1324435876019-0000)/)。 当我将时钟设置为 EST 时区时,我的代码
1. 原始单据与实体之间的关系 可以是一对1、一对多、多对多的关系。在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体。在特殊情况下,它们可能是一对多或多对一的关系,即一张原
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章服务器五大相关基础知识【转】由作者收集整理,如果你对这篇文章有兴趣,记得
Google Apps 脚本 - Gmail 是否会实现 GmailMessage (GmailThread) .getAsPdf() 方法?预期输出与 Gmail 中可用的打印为 PDF 的输出相同
有一个需求是要在一个云监控的状态值中存储多个状态(包括可同时存在的各种异常、警告状态)使用了位运算机制在一个int型中存储。 现在监控日志数据量非常大(亿级别)需要对数据按每小时、每天进行聚合,供
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章1张图看懂RAID功能,6张图教会配置服务器【转】由作者收集整理,如果你
我正在使用 FFMPeg(版本 ffmpeg-20170330-ad7aff0-win64-static)将 RTSP 转换为 .m3u8。 命令是: ffmpeg -rtsp_transport t
我有一个 JTree使用 DefaultTreeModel 的对象作为模型,我添加/删除与该模型相关的节点。 此时,我需要在图形界面中显示树结构,例如 JPanel .如何映射 DefaultTree
我当前正在接收一个文件并将其存储到 NSString 中。然后,我从字符串中创建一个数组并将其呈现在 TableView 中。这在一定程度上有效。我目前收到的数据如下: 公司名称|帐户代码\r\n公司
我需要创建 NSImage cocoa 对象的 base64 字符串表示形式。处理这个问题的最佳方法是什么,苹果文档似乎在这个主题上有点短(或者我只是找不到它)。 Base64 编码从外面看起来相当复
JS 中的 .toISOString() 函数给我这样的字符串: 2015-06-14T20:00:00:000Z 我需要它是这样的: 2015-06-14T20:00:00Z JS 中是否有其他函数
我正在尝试使用 JavaScript 转换 COLORREF: COLORREF : When specifying an explicit RGB color, the COLORREF value
我在这里遇到了这个代码的问题,只是想制作一个小计算器: 打包申请; import javafx.event.ActionEvent; import javafx.scene.control.TextF
我想要做的是能够通过本地PC上的USS通过sshfs挂载主机上的一些文件。我可以做到这一点,但 sshfs 不能直接完成从 EBCDIC 到 ascii/unicode 的转换。有没有我可以设置的标志
我正在尝试在 python 中将一堆 Visio 文件转换为 pdf。我已经引用了这个.doc to pdf using python并编写了以下代码: import comtypes.client
我是一名优秀的程序员,十分优秀!