-
我使用 Speech Recognizer Intent 来获取 User Input ,然后再翻译成文本。但是我想让intent 连续的获取
user input 然后翻译成文本,来看用户是否说了某个词。现在的代码能实现,但是每次程序开始侦听输入时,电话就会发出短期的警笛声,准备输入。
有什么方法来删除已经播放过的又一次播放的声音?
代码:import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity implements OnClickListener { //private TextView mText; private SpeechRecognizer sr; private static final String TAG = "MyStt3Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ToggleButton speakButton = (ToggleButton) findViewById(R.id.CarMode); //mText = (TextView) findViewById(R.id.textView1); speakButton.setOnClickListener(this); sr = SpeechRecognizer.createSpeechRecognizer(this); sr.setRecognitionListener(new listener()); } class listener implements RecognitionListener { public void onReadyForSpeech(Bundle params) { Log.d(TAG, "onReadyForSpeech"); } public void onBeginningOfSpeech() { Log.d(TAG, "onBeginningOfSpeech"); } public void onRmsChanged(float rmsdB) { Log.d(TAG, "onRmsChanged"); } public void onBufferReceived(byte[] buffer) { Log.d(TAG, "onBufferReceived"); } public void onEndOfSpeech() { Log.d(TAG, "onEndofSpeech"); } public void onError(int error) { Log.d(TAG, "error " + error); Toast.makeText(getApplicationContext(), "What was that?", Toast.LENGTH_SHORT).show(); sr.cancel(); speechIntent(); } public void onResults(Bundle results) { //String str = new String(); Log.d(TAG, "onResults " + results); ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); Toast.makeText(getApplicationContext(), data.get(0), Toast.LENGTH_SHORT).show(); sr.cancel(); speechIntent(); //mText.setText("results: "+String.valueOf(data.size())); } public void onPartialResults(Bundle partialResults) { Log.d(TAG, "onPartialResults"); } public void onEvent(int eventType, Bundle params) { Log.d(TAG, "onEvent " + eventType); } } public void onClick(View v) { if (v.getId() == R.id.CarMode) { speechIntent(); } } public void speechIntent() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"com.example.freestyleandroid"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); sr.startListening(intent); Log.i("111111","11111111"); } }
-
-
-
-
-
android 语音识别时如何获取识别的状态?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576public
class
MyStt3Activity
extends
Activity
implements
OnClickListener {
private
TextView mText;
private
SpeechRecognizer sr;
private
static
final
String TAG =
"MyStt3Activity"
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button speakButton = (Button) findViewById(R.id.btn_speak);
mText = (TextView) findViewById(R.id.text);
speakButton.setOnClickListener(
this
);
sr = SpeechRecognizer.createSpeechRecognizer(
this
);
sr.setRecognitionListener(
new
listener());
}
class
listener
implements
RecognitionListener
{
public
void
onReadyForSpeech(Bundle params)
{
Log.d(TAG,
"onReadyForSpeech"
);
}
public
void
onBeginningOfSpeech()
{
Log.d(TAG,
"onBeginningOfSpeech"
);
}
public
void
onRmsChanged(
float
rmsdB)
{
Log.d(TAG,
"onRmsChanged"
);
}
public
void
onBufferReceived(
byte
[] buffer)
{
Log.d(TAG,
"onBufferReceived"
);
}
public
void
onEndOfSpeech()
{
Log.d(TAG,
"onEndofSpeech"
);
}
public
void
onError(
int
error)
{
Log.d(TAG,
"error "
+ error);
mText.setText(
"error "
+ error);
}
public
void
onResults(Bundle results)
{
String str =
new
String();
Log.d(TAG,
"onResults "
+ results);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for
(
int
i =
0
; i < data.size(); i++)
{
Log.d(TAG,
"result "
+ data.get(i));
str += data.get(i);
}
mText.setText(str);
}
public
void
onPartialResults(Bundle partialResults)
{
Log.d(TAG,
"onPartialResults"
);
}
public
void
onEvent(
int
eventType, Bundle params)
{
Log.d(TAG,
"onEvent "
+ eventType);
}
}
public
void
onClick(View v) {
if
(v.getId() == R.id.btn_speak) {
sr.startListening(
new
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
}
}
-
-
-
调用讯飞API,写一个语音小程序,准备说话时总会出现错误10111,上官网查了一下,说是没有初始化 有这么一个错误
-
public
class
VoiceToWord
extends
Activity
implements
OnInitListener {
private
static
String TAG =
"Demo"
;
private
Button button_v=
null
;
private
EditText et =
null
;
TextToSpeech speak =
null
;
// 定义tts
//听写语音对象
private
SpeechRecognizer mIat;
private
RecognizerDialog iatDialog;
private
Toast mToast;
private
SharedPreferences mSharedPreferences;
int
ret =
0
;
// 函数调用返回值
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
et = (EditText)findViewById(R.id.et);
button_v = (Button)findViewById(R.id.speakint);
//初始化识别对象
mIat = SpeechRecognizer.createRecognizer(
this
, mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
iatDialog =
new
RecognizerDialog(
this
, mInitListener);
mSharedPreferences = getSharedPreferences(
"com.example.designbs"
,
Activity.MODE_PRIVATE);
mToast = Toast.makeText(
this
,
""
, Toast.LENGTH_SHORT);
speak =
new
TextToSpeech(
this
,
this
);
button_v.setOnClickListener(
new
voiceListener());
}
class
voiceListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
et.setText(
null
);
// 清空显示内容
// 设置参数
setParam();
boolean
isShowDialog = mSharedPreferences.getBoolean(
"iat_show"
,
true
);
if
(isShowDialog) {
// 显示听写对话框
iatDialog.setListener(recognizerDialogListener);
iatDialog.show();
showTip(
"开始听写"
);
}
else
{
// 不显示听写对话框
ret = mIat.startListening(recognizerListener);
if
(ret != ErrorCode.SUCCESS) {
showTip(
"听写失败,错误码:"
+ ret);
}
else
{
showTip(
"开始听写"
);
}
}
}
}
/**
* 听写监听器。
*/
private
RecognizerListener recognizerListener =
new
RecognizerListener() {
@Override
public
void
onBeginOfSpeech() {
showTip(
"开始说话"
);
}
@Override
public
void
onError(SpeechError error) {
showTip(error.getPlainDescription(
true
));
}
@Override
public
void
onEndOfSpeech() {
showTip(
"结束说话"
);
}
@Override
public
void
onEvent(
int
eventType,
int
arg1,
int
arg2, String msg) {
}
@Override
public
void
onResult(RecognizerResult results,
boolean
isLast) {
String text = JsonParser.parseIatResult(results.getResultString());
et.append(text);
//添加听写的话
et.setSelection(et.length());
if
(isLast) {
// TODO 最后的结果
}
}
@Override
public
void
onVolumeChanged(
int
volume) {
showTip(
"当前正在说话,音量大小:"
+ volume);
}
};
/**
* 初始化监听器。
*/
private
InitListener mInitListener =
new
InitListener() {
@Override
public
void
onInit(
int
code) {
Log.d(TAG,
"SpeechRecognizer init() code = "
+ code);
if
(code == ErrorCode.SUCCESS) {
findViewById(R.id.speakint).setEnabled(
true
);
}
}
};
/**
* 听写UI监听器
*/
private
RecognizerDialogListener recognizerDialogListener =
new
RecognizerDialogListener() {
public
void
onResult(RecognizerResult results,
boolean
isLast) {
String text = JsonParser.parseIatResult(results.getResultString());
et.append(text);
boolean
flags1=text.contains(
"电话"
);
//是否含有电话
if
(flags1){
Intent intent=
new
Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Contacts.People.CONTENT_URI);
startActivity(intent);
}
boolean
flags2=text.contains(
"短信"
);
//是否含有短信
if
(flags2){
Intent intent=
new
Intent(Intent.ACTION_VIEW);
intent.setType(
"vnd.android-dir/mms-sms"
);
startActivity(intent);
}
et.setSelection(et.length());
}
/**
* 识别回调错误.
*/
public
void
onError(SpeechError error) {
showTip(error.getPlainDescription(
true
));
}
};
private
void
showTip(
final
String str) {
runOnUiThread(
new
Runnable() {
@Override
public
void
run() {
mToast.setText(str);
mToast.show();
}
});
}
/**
* 参数设置
*
* @param param
* @return
*/
@SuppressLint
(
"SdCardPath"
)
public
void
setParam() {
String lag = mSharedPreferences.getString(
"iat_language_preference"
,
"mandarin"
);
if
(lag.equals(
"en_us"
)) {
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE,
"en_us"
);
}
else
{
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE,
"zh_cn"
);
// 设置语言区域
mIat.setParameter(SpeechConstant.ACCENT, lag);
}
// 设置语音前端点
mIat.setParameter(SpeechConstant.VAD_BOS,
mSharedPreferences.getString(
"iat_vadbos_preference"
,
"4000"
));
// 设置语音后端点
mIat.setParameter(SpeechConstant.VAD_EOS,
mSharedPreferences.getString(
"iat_vadeos_preference"
,
"1000"
));
// 设置标点符号
mIat.setParameter(SpeechConstant.ASR_PTT,
mSharedPreferences.getString(
"iat_punc_preference"
,
"1"
));
// 设置音频保存路径
mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH,
"/sdcard/iflytek/wavaudio.pcm"
);
}
@Override
public
void
onInit(
int
status) {
// TODO Auto-generated method stub
speak.setLanguage(Locale.US);
// 初始化TTS组件,设置语言为US英语
}
}
-